torsuntsu Posted October 25, 2013 Share Posted October 25, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/283285-get-value-from-dropdownlist/ Share on other sites More sharing options...
Ch0cu3r Posted October 25, 2013 Share Posted October 25, 2013 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/283285-get-value-from-dropdownlist/#findComment-1455450 Share on other sites More sharing options...
torsuntsu Posted October 25, 2013 Author Share Posted October 25, 2013 Tanks Ch0cu3r, but the problem is still there. How can i pass the value selected by the user to a variable. That would help. Then i could use the variable to further do database queries. This site rocks.... Many thanks, TorSunTsu Quote Link to comment https://forums.phpfreaks.com/topic/283285-get-value-from-dropdownlist/#findComment-1455453 Share on other sites More sharing options...
Ch0cu3r Posted October 25, 2013 Share Posted October 25, 2013 The variable $_POST['utilizadores'] will contain the selected value when the form has been submitted. The snippet of code I posted should have selected the same item when the form was submitted. Quote Link to comment https://forums.phpfreaks.com/topic/283285-get-value-from-dropdownlist/#findComment-1455457 Share on other sites More sharing options...
Solution torsuntsu Posted October 25, 2013 Author Solution Share Posted October 25, 2013 Ch0cu3r, you saved my life. You rock...Problem solved. Many thanks...:-) Quote Link to comment https://forums.phpfreaks.com/topic/283285-get-value-from-dropdownlist/#findComment-1455458 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.