Hi,

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'>    

<?php 

$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>";
$studentrow++;
}
?>

<input type="submit" value="Select Student">
</select>
</form>

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


Thanks.

Recommended Answers

All 5 Replies

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:*/
			,htmlentities($studentrow,ENT_QUOTES)
			,htmlentities($row['studentID'],ENT_QUOTES)
			,htmlentities($row['studentID'],ENT_QUOTES)
			,htmlentities($row['firstName'],ENT_QUOTES)
			,htmlentities($row['lastName'],ENT_QUOTES)
		);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.