Jump to content

Get value from dropdownlist


Go to solution Solved by 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>';
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.