I have populated a dropdown menu using php pdo from sqlite database. How would i go about reading what the user has selected in the dropdown menu. so i can do something like:

$userSelected = [whatever user selected in the dropdown menu];

my code is as follows (part of it):

<form name="placementStudents" method="post"><br />
<select name = 'studentrow'>    


$db = new PDO('sqlite:mydb.sqlite');

$getStudents= $db ->query("SELECT this part works");

$rowarray2 = $getStudents->fetchAll();
$studentrow = 0;

foreach($rowarray2 as $row)
echo "<option value = $studentrow>$row[studentID] $row[firstName] $row[lastName]</option>";

<input type="submit" value="Select Student">

(removed the sql select statement as it was quite long, but it does work)


7 Years
Discussion Span
Last Post by hielo

You need to close your SELECT. Then to read the selected value upon post:

if( isset($_POST) && !empty($_POST))
        $userSelected = $_POST['studentrow'];

Sorry im quite new to php/sqlite, but by close do you mean to close the database connection or have i missed something in the coding.

Thanks again.


No. What I meant was that you opened your HTML <SELECT ...> tag, but you did NOT close it ( </SELECT> )


Well it kinda works but im not getting any text, only numbers. If the first value in the dropdown is selected -> echo $userSelected returns 0
if the second value is selected it returns 1.


It will show you, whatever it is you are putting in the VALUE of each <OPTION> . If you want more than that, you can create a delimited value. For example, if you wanted the $stuendrow AND the $row, then make your value something like value="34_23" and when you get that on the server, use the explode() function to break it at the "_":

echo sprintf('<option value="%s_%s">%s %s %s</option>'/*each of those %s will be substituted (left->right) with the following:*/
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.