Passing variables in <select>

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
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Passing variables in <select>

Post by 3hhh »

Hi,

I have the following code:

<select name=test>
<option value=first result>first result</option>
<option value=second result>second result</option>
</select>

When I do a $_POST['test'], it returns "first" instead of "first result" and "second" instead of "second result". Why is that so? Kindly advise. Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Passing variables in <select>

Post by Christopher »

You should quote all property values:

Code: Select all

<select name="test">
  <option value="first result">first result</option>
  <option value="second result">second result</option>
</select>
(#10850)
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Re: Passing variables in <select>

Post by 3hhh »

arborint wrote:You should quote all property values:

Code: Select all

<select name="test">
  <option value="first result">first result</option>
  <option value="second result">second result</option>
</select>
Hi,

Yes I did that. This is my actual code:

echo "<select name=\"venue\" class=\"normal\">";
if ($result=mysql_query($sql)) {
while ($row=mysql_fetch_row($result)) {
echo "<option value=".$row[0].">".$row[0]."</option>";
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Passing variables in <select>

Post by Christopher »

No, you did not have quotes around the values:

Code: Select all

            echo '<select name="venue" class="normal">';
                if ($result=mysql_query($sql)) {
                  while ($row=mysql_fetch_row($result)) {
                    echo '<option value="'.$row[0].'">'.$row[0].'</option>';
                  }
(#10850)
murp3433
Forum Newbie
Posts: 4
Joined: Fri Sep 19, 2008 11:02 pm

Re: Passing variables in <select>

Post by murp3433 »

arborint wrote:No, you did not have quotes around the values:

Code: Select all

            echo '<select name="venue" class="normal">';
                if ($result=mysql_query($sql)) {
                  while ($row=mysql_fetch_row($result)) {
                    echo '<option value="'.$row[0].'">'.$row[0].'</option>';
                  }

Also if you alternate between single and double quotes you wont have to use escapes. Not a big deal just good practice as it makes your code more readable.

echo '<select name="venue" class="normal">';
3hhh
Forum Newbie
Posts: 22
Joined: Wed Sep 10, 2008 12:02 pm

Re: Passing variables in <select>

Post by 3hhh »

Thanks! It works fine now... Didn't know about the ' and ".... :)
Post Reply