Wander
Wander

Reputation: 177

Getting option name php

I am new to PHP so bear with me. I have some code like this -

echo ('<select name="fullname">');
while($row = mysql_fetch_array($dorequest)) {
  echo ('<option name="' . $row['ID'] . '">' . $row['firstname'] . " " . $row['lastname'] .  '</option>'); 
}
echo('</select>');

Which works fine. When I submit the form I want to get the ID of the selected option ( $row['ID'] ). I can only get the content of the select box. Any ideas how I might do this?

Upvotes: 0

Views: 2814

Answers (3)

Daryl Gill
Daryl Gill

Reputation: 5524

If your using post for your select form, then during the while loop you could add

$ShowID = $rows['ID'];

Then on your query during the post function you could use the $ShowID on your query or echo it out using echo $ShowID;

Upvotes: 0

Mike Brant
Mike Brant

Reputation: 71384

You should being using:

echo ('<option value="' . $row['ID'] . '">' . $row['firstname'] . " " . $row['lastname'] .  '</option>');

Note I changed name to value. Options have values, not names. The input name is determined by the property in the select.

Upvotes: 0

Use a value attribute instead of name:

echo ('<option name="' . $row['ID'] . '">' . $row['firstname'] ...
                ^-------HERE, use "value" instead of "name"

As the name for the sending variable is already set in your select <select name="fullname"> what you need to specify in the <option> is the value you want to send.

Upvotes: 6

Related Questions