<?Php
require "config.php"; // Database Connection
$sql="select id, name from student ";
$row=$dbo->prepare($sql);
$row->execute();
$result=$row->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(array('data'=>$result));
?>
$.getJSON("student-data.php", function(return_data){
Now using each loop we will create one string to add options to our listbox.
$.each(return_data.data, function(key,value){
$("#student").append(
"<option value=" + value.id +">"+value.name+"</option>"
);
});
In the HTML part we have the student dropdown list. Here is the complete code of student-list.php page.
<html>
<head>
<title>plus2net demo scripts using JQuery</title>
</head>
<script src="//ajax.googleapis.com/ajax/libs/
\jquery/1.8.1/jquery.min.js"></script>
<body>
<script>
$(document).ready(function() {
//////////////////////
$.getJSON("student-data.php", function(return_data){
$.each(return_data.data, function(key,value){
$("#student").append(
"<option value=" + value.id +">"+value.name+"</option>"
);
});
});
//////////////////////
});
</script>
<select name=student id=student>
</select>
</body>
</html>
$('#student').change(function(){
//var st=$('#student option:selected').text();
var st=$('#student').val();
alert(st);
});
Inside your zip file this code is kept in a separate file student-list2.php file.
Double dropdown list box for category and subcategory
10-12-2021 | |
Hi, I've followed your code and it produces exactly the promised result with my own data. But for me this was just the first step - and difficult enough. What I now try to do is to have the option of sorting the list with studen name and first name alternatingly. I understand that postJSON does not exist. So I created 2 student-list.php fetching the data once sorted according to first name, one last name. I inserted on top of the <select> statement two buttons with id=”first” and id=”last”. Their visibility toggles between First Name and Last Name. I used your code as a callback function in $(this).show() for both buttons, but the contents of the dropdown list does not change. Thanks if you can offer any help on this. |
27-12-2021 | |
This is done at the backend, based on user selection of button with id=First or id=Last at the backend the query is changed to get the in the required order by using ORDER BY query. |
Most Popular JQuery Scripts | |
1 | Two dependant list boxes |
2 | Calendar with Date Selection |
3 | Data change by Slider |
4 | Show & Hide element |