Get Selected Value Of Dropdownlist

I have a dropdownlist of a list of student id. When user selects any of the student id, the particular student’s details will be displayed. The problem is how do I get the selected student id by the user?

Here’s my php code (view file):




<p style="margin-left:10px"> List of Students:  

<?php

	$connection=Yii::app()->db;

    $sql="SELECT student_no FROM student";

    $command=$connection->createCommand($sql);

	$dataReader=$command->query();	

	

	echo "<select name='student'><option value=''>Select student</option>";

	while(($row=$dataReader->read())!==false)

	{  

		echo "<option value='$row[student_no]'>$row[student_no]</option>";

	}

		echo "</select>";

?>	

</p>


<h3 style="margin-left:10px">Student Details</h3>

<hr/>


<div style="margin-left:10px">

<?php

    $connection=Yii::app()->db;

    $selected=$_POST['student'];

    $sql1="SELECT `name`, `student_no`, `intake`, `program_code` FROM `student` WHERE `student_no`='$selected'";

    $command1=$connection->createCommand($sql1);

	$dataReader1=$command1->query();	

	$row1=$dataReader1->read();

	

    echo '<h5><strong> Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['name']. '</strong></h5>';

	echo '<h5><strong> Student No:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['student_no']. '</strong></h5>';

	echo '<h5><strong> Intake:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['intake']. '</strong></h5>';

	echo '<h5><strong> Programme:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['program_code']. '</strong></h5>';

?></div>



Can anyone help me how to pass the selected student id to the sql?? Is it will not be working in Yii Framework if I use the code above?

$sql1=“SELECT name, student_no, intake, program_code FROM student WHERE student_no=[color=”#FF0000"]’$selected’";[/color]

if student_no field is integer type then remove single quotes from sql, your query would be like:




  $sql1="SELECT `name`, `student_no`, `intake`, `program_code` FROM `student` WHERE `student_no`=$selected";



Hi Absar A Hasan, thank you so much for the reply. I have tried to remove the single quotation but it returned an error as well. The error is ‘[color="#FF0000"]Undefined index: student[/color]’. How to solve this?? Anything that I missed out?

Are you getting value in $selected=$_POST[‘student’]; try to debug by printing it on browser after submitting form. it seems that post value is not defined, if this contains value then echo $sql1 and run Query on mysql like phpmyadmin if using you will come to know about result.

I have tried to do in javascript and I can get the selected value from dropdownlist. But the problem is how am I going to pass the variable from javascript to the sql1 ???

  1. Get value from selected dropdownlist by user

  2. Pass the variable ([color="#FF0000"]var strSel[/color]) to sql1

($sql1="SELECT name, student_no, intake, program_code FROM student WHERE student_no=[color="#FF0000"]strSel[/color] (get the variable of strSel from javascript)";

Hope that you can understand what I am trying to do. Thank you.




<script>

function GetSelectedItem()

{

    var e = document.getElementById("student");

    var strSel = e.options[e.selectedIndex].value;

    //alert(strSel);

}

</script>


<p style="margin-left:10px"> List of Students:  

<?php

	$connection=Yii::app()->db;

    $sql="SELECT student_no FROM student";

    $command=$connection->createCommand($sql);

	$dataReader=$command->query();	

	

	echo "<select id='student'><option value=''>Select student</option>";

	while(($row=$dataReader->read())!==false)

	{  

		echo "<option value='$row[student_no]'>$row[student_no]</option>";

	}

		echo "</select>";

		 

?><br>	

<button onClick="GetSelectedItem();">Process</button>

</p>


<h3 style="margin-left:10px">Student Details</h3>

<hr/>


<div style="margin-left:10px">

<?php

    $connection=Yii::app()->db;

	//$selected=$_POST['student'];

    $sql1="SELECT `name`, `student_no`, `intake`, `program_code` FROM `student` WHERE `student_no`=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?(get the variable of strSel from javascript)";

    $command1=$connection->createCommand($sql1);

	$dataReader1=$command1->query();	

	$row1=$dataReader1->read();

	

    echo '<h5><strong> Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['name']. '</strong></h5>';

	echo '<h5><strong> Student No:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['student_no']. '</strong></h5>';

	echo '<h5><strong> Intake:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['intake']. '</strong></h5>';

	echo '<h5><strong> Programme:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['program_code']. '</strong></h5>';

?></div><hr>



You can not use JavaScript variable strSel in php sql variable directly.

Your first attempt was right with given condition





<div style="margin-left:10px">

<?php

if(isset($_POST['student']) && !empty($_POST['student'])){

    $connection=Yii::app()->db;


     $selected=$_POST['student'];

    $sql1="SELECT `name`, `student_no`, `intake`, `program_code` FROM `student` WHERE `student_no`=<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?(get the variable of strSel from javascript)";

    $command1=$connection->createCommand($sql1);

        $dataReader1=$command1->query();        

        $row1=$dataReader1->read();

        

    echo '<h5><strong> Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['name']. '</strong></h5>';

        echo '<h5><strong> Student No:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['student_no']. '</strong></h5>';

        echo '<h5><strong> Intake:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['intake']. '</strong></h5>';

        echo '<h5><strong> Programme:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' .$row1['program_code']. '</strong></h5>';

}

?></div><hr>



use above and let me know you scenario what you want to implement if not resolved

I am still can’t get the result that I want. Initially, my page looks like the attachment. 5513

student.png

1.I wish to let user to choose any of the student’s id from the dropdownlist to view the particular student’s details.

  1. When I add the following code, there is no error but student’s details is not shown after I click any of the student’s id.



if(isset($_POST['student']) && !empty($_POST['student']))

{

  $selected=$_POST['student'];

}



Hope that what I’m explaining is clear enough. Thank you so much for your guidance. Is there anything I missed out??? I can’t get the selected value and put it into sql1 condition.

How do you do the submit (post)?

If you do not do that, the $_POST[] has never get the values that you need.