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?
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
Get value from selected dropdownlist by user
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: ' .$row1['name']. '</strong></h5>';
echo '<h5><strong> Student No: ' .$row1['student_no']. '</strong></h5>';
echo '<h5><strong> Intake: ' .$row1['intake']. '</strong></h5>';
echo '<h5><strong> Programme: ' .$row1['program_code']. '</strong></h5>';
?></div><hr>
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.