Retrieving Drop down/Radio button values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JaredR672
Forum Newbie
Posts: 2
Joined: Wed Jul 09, 2003 4:21 pm

Retrieving Drop down/Radio button values

Post 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
Duke of PHP
Forum Newbie
Posts: 5
Joined: Mon Apr 21, 2003 2:20 pm

Post 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 :P
JaredR672
Forum Newbie
Posts: 2
Joined: Wed Jul 09, 2003 4:21 pm

Post 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.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

I think they meant that on the form page, you would use

Code: Select all

&lt;select name="state"&gt;
              &lt;option value=""&gt;Choose&lt;/option&gt;
              &lt;option value="AK"&gt;AK&lt;/option&gt;
              &lt;option value="AL"&gt;AL&lt;/option&gt;
              &lt;option value="AR"&gt;AR&lt;/option&gt;
              &lt;option value="AZ"&gt;AZ&lt;/option&gt;
              &lt;option value="CA"&gt;CA&lt;/option&gt;
              &lt;option value="CO"&gt;CO&lt;/option&gt;
...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! :)
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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.
Post Reply