0

I'm searching a way to retrieve an ID corresponding to a 'select' without execute any other query when I select an item from Database:

I use the select item in a form.

Here is the way I select some names from a table from Database:

    $sql = "SELECT ID, Name,Surname FROM Table;";
    $result = mysql_query($sql);
    if(!$result) die ('Unable to run query:'.mysql_error());
    $la = "<SELECT name='names'>";
    $la .= "<OPTION selected='selected' disabled='disabled' >Choose a name</OPTION>";

    while(list($id, $name) = mysql_fetch_row($result)) {
        $selectnames .= "<OPTION >$name</OPTION>";
    }
    $selectnames .= "</SELECT>";

I want to know the ID corresponding to the '$selectnames' I select from a form,

Thanks!

4

1 回答 1

0

You need to set the value of the option to $id:

while(list($id, $name) = mysql_fetch_row($result)) {
    $selectnames .= "<OPTION value='$id'>$name</OPTION>";
}

And then when the form is posted you can check $_POST['names'] to get the ID. As you might have noticed, if you don't specify the value then $_POST['name'] will contain the $name value rather than the $id value.

于 2013-03-30T21:07:35.337 回答