Page 1 of 1
Retrieving Drop down/Radio button values
Posted: Wed Jul 09, 2003 4:21 pm
by JaredR672
I'm creating a form for users to join a membership site.
http://phippsrealty2.com/test/register2.php
I created the form so that the process page would check to make sure each field had a value, if not send it back to the form page to be filled in. I made it so that it would echo the values already submitted in the text fields. Now I can not get the values of the radio buttons and the drop downs to appear. Otherwise this is very annoying for the user to keep re entering information he has already submitted.
Thanks in advance for any viable solution
Posted: Wed Jul 09, 2003 4:50 pm
by Duke of PHP
Okay, for the dropdown menu-
Say we had one like this: <select name=grocery><option value=banana>Banana</option><option value=apples>Apples</option>etc.etc.</select>
You see how I had <select name=grocery>? Thats the name of the menu, and how we will work with it. Now, if I wanted to echo whatever the user selected in that dropdown menu, whether it bre apples or bananas, I would do echo "$grocery"; since I named the menu grocery.. And as far as radio buttons go, look at webmonkey.com

Posted: Wed Jul 09, 2003 5:18 pm
by JaredR672
Code: Select all
<select name="state">
<option value="">Choose</option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
Code: Select all
<select name="<?php echo $state; ?>">
<option value="">Choose</option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
I dont believe thats what you mean but thats what I came up with and it did not work
*edit* After reading over your post I'm not positive we are talking about the same thing. I know how to print or echo the value if its submitted. But what I'm trying to accomplish now is when a user fills out a form he forgets a field and is sent back to that page where he forgot a field. Instead of retyping them all, I want all the values to appear so all he has to do is edit the one field he forgot.
Posted: Wed Jul 09, 2003 5:35 pm
by trollll
I think they meant that on the form page, you would use
Code: Select all
<select name="state">
<option value="">Choose</option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
...and then in the php that processes the form you would reference the user's state with $state, like this:
Code: Select all
echo "Thank you for registering, you live in $state.";
That would print out whatever state the user had selected in place of $state. Or you could insert the value of $state into a database, write it to a file, send it to a cookie, etc.
Naming the select with what the user had selected will not work. What I do when I need to have the form "remember" the user's input: set up a loop to cycle through each of the options and if the user has selected one, write it as selected.
Code: Select all
$states = Array("AK", "AL", "AR"); // array holding possible values
echo "<select name="state">";
for ($i=0;$i<count($states);$i++) {
// $state got passed from the form
if ($state == $states[$i]) {
print "<option value="" . $states[$i] . "" selected="selected">";
} else {
print "<option value="" . $states[$i] . "">";
}
}
echo "</select>";
This would also work when the user hasn't used the form yet, as $state will not have a value and won't equal any of the states in the array. Hope that helps!

Posted: Wed Jul 09, 2003 5:51 pm
by trollll
As a quick P.S., you can also use the same logic to "remember" the user's selection of radio buttons and check boxes. Just use echo "<input type=\"radio\" name=\"" . radios[$i] . "\" value=\"" . radios[$i] . "\" checked=\"checked\" />"; instead of the option.