PDA

View Full Version : Populating a form field from a drop down



munkynpunky
09-24-2006, 01:45 AM
Hey,

Ok, i put this in php by mistake...

can anyone help please?


Hey guys,

I'm trying to use one drop down menu - and select an item, once selected shows the cost up instantly in a form field nest to the drop down..

There are maximum of 5 items, each with 5 different prices.

i dont know if this is the right forum, but im writting in php, so thought id try here first :)

Thanks for your help




This isn't php... it's javascript.
The trick here is making the php output the right javascript code. Depending on the complexity of your php, and how much of the html it generates, it could be easy or hard. Hopefully, it's not too hard.

Anyway, for the javascript code itself...

<select onChange="result.value=this.value">
<option value="1">One</option>
<option value="2">Two</option>
</select>
<input type="text" id="result">

Note: You might want to set an initial value in the textfield that matches the pre-selected option in the dropdown.

munkynpunky
09-24-2006, 01:45 AM
Hmmm ok, right, well its basically

year one = 3.25
year two = 6.50
year three = 9.75

and if i select year one i want to see 3.25 in a different readonly box next to the year selection...

djr33
09-24-2006, 02:23 AM
Well, just change the values. Simple as that.

If you wanted to do math, you can do that in JS as well.


As for readonly... I'm not sure what the code is for that.

I just tried:
<input type="text" id="result" readonly>
And that worked, though I'm not sure if that's the "official" way to do it. Easy, though ;)

munkynpunky
09-24-2006, 11:52 AM
Well, just change the values. Simple as that.

If you wanted to do math, you can do that in JS as well.


As for readonly... I'm not sure what the code is for that.

I just tried:
<input type="text" id="result" readonly>
And that worked, though I'm not sure if that's the "official" way to do it. Easy, though ;)

Oh ok, so do i not need any code..
sorry for the stupid questions but i dont know javascript very well at all..

What i need is

the user selects eg. 1 year - 3.25
then in a second drop down selects : item 1 - 25.00

then i want a field that adds Drop down one + drop down two,
and displays the result - 28.25

munkynpunky
09-24-2006, 12:19 PM
hmm, ok, yeah just sorted that, thank you :)



if ( $item == "item1, item2" ){


i tried


if ( $item == "item1", "item2" ){

and


if ( $item == "item1" OR "item2" ){

now, this doesnt work, what i want is if item is item1 or item2 then do this:
xxxx

blm126
09-24-2006, 12:48 PM
if($item == 'item1' || $item == 'item2'){
//Do stuff here
}

djr33
09-24-2006, 01:11 PM
Do you mean to be using php now?
Or is this still a javascript operation?

munkynpunky
09-24-2006, 01:55 PM
excellent, all working now thank you!

now im having a problem passing info via a mail.php

i have an ordering page, --> mail.php --> payment page.

the info goes to the mail.php page but not to the payment page.. can anyone help?

blm126
09-24-2006, 02:59 PM
You can probably rig up a redirect, or some hidden fields(remeber to revalidate), but first I need to know what variable you want sent to mail.php

mwinter
09-24-2006, 04:08 PM
As for readonly... I'm not sure what the code is for that.

I just tried:
<input type="text" id="result" readonly>
And that worked, though I'm not sure if that's the "official" way to do it. Easy, though ;)

Why didn't you look at the HTML specification? Read-only controls (http://www.w3.org/TR/html4/interact/forms.html#h-17.12.2)

Mike

djr33
09-24-2006, 09:53 PM
According to that, it's either readonly or disabled, and looks like only readonly will send it's value through the form, which might be helpful.

munkynpunky
09-24-2006, 11:11 PM
Hey,

Ok, Thank you for your input... I have managed to get my pages together now..

Now, the second thing...

I am connecting to Paypal, but what i need to do, is pass everything to a mail.php for first, so that i can email myself before the payment comes through, that way, if the payment fails, i still know that someone tried to place an order...

Basically this is a submit one form to two documents/locations...



echo '<form method="POST" action="mail.php" name="emailmefirst">';

echo '<form name="agreeform" onSubmit="return defaultagree(this)" action="https://www.paypal.com/cgi-bin/webscr" method="post">';

djr33
09-25-2006, 12:23 AM
Nope. You can't submit one form to two locations.
I suppose you could theoretically do some weird type of multi-frame setup and send to both using JS and/or use a new window for one, but that's just a pain.

The way to do it is make mail.php and have a duplicate of your form (with hidden fields instead of visable ones like before), then send that data on to the paypal site that way. Echo the values of the $_POST data into their respective hidden fields on the mail page.

munkynpunky
10-03-2006, 11:51 PM
Hi guys,

Thanks for all your help.

This has now been resolved... I dont know what i was doing wrong, but all is ok

:)