Jump to content

Get value from dropdownlist


torsuntsu

Recommended Posts

Good evening,

i have a problem with some code. I´ve created a dropdownlist that gests it´s values from a database. 

The dropdownlist is being populated without problems. My problem is that i must get the selected value from the

dropdown to complete my task. But i don´t know how. I´d appreciate some help. I send some of the code:

 

echo'  <form action="permissions.php" method="post" id="form">

     <table><tr><td>Seleccionar Utilizador:</td><td><select name="utilizadores" id="utilizadores">';
$query = mysql_query("SELECT * FROM `users`");
echo'<option value="">****** Utilizador *******</option>';
 while(($row = mysql_fetch_array($query))!== false)
{  
    echo '<option value="'.$row["user_id"].'">'.$row["first_name"].' '.$row["last_name"].'</option>';
}
echo '</select></td>
 
</tr>
</table>';
 
I must get the value selected by the user, without posting because i have to use that value for a database search, so i can
display further values. Appreciated some help. I´m desperate :-(
 
Many thanks,
TorSunTsu
Link to comment
https://forums.phpfreaks.com/topic/283285-get-value-from-dropdownlist/
Share on other sites

You need to check if the posted value is the same as the value you're adding to the list, then you can set the selected attribute to the option.

while(($row = mysql_fetch_array($query))!== false)
{
    $selected = (isset($_POST['utilizadores']) && $row['user_id'] == $_POST['utilizadores']) ? ' selected' : '';
    echo '<option value="'.$row["user_id"].'"'.$selected.'>'.$row["first_name"].' '.$row["last_name"].'</option>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.