I have small project of PHP and JavaScript, which used to registering students on the MySql database and it does perfectly, but I need when user search/type certain name on it, example if he/she type name like "John" it has to show drop down of names which are relate with letter "J", I tried but it doesn't work. I will show you step by step What I did.
1: This is html form which user use to enter students Marks, on field name is where I want when user type name on text box, it has to show related names:
Note: I will show you few fields.
<form action="#" method="POST" id="name" name="name"
enctype="multipart/form-
data">
<div class="row">
<div class="col-sm-6">
<div class="panel panel-default">
<div class="panel-body">
<div class="form_sep">
<label for="reg_input_name" class="req">Admition Number</label>
<input type="text" id="idnum" name="idnum" class="form-control"
data-required="true" >
</div>
<div class="form_sep">
<label for="reg_textarea_message" class="req">Name</label>
<input type="text" name="name" id="name" cols="30" rows="4"
class="form-control"></div>
</div>
</div>
</div></div></form>
2:This is JavaScript codes and jQuery plugin which I place down on the same html form.
<script src="../jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.get('getStudents.php', function(data){
var students = JSON.parse(data);
$('#name').autocomplete({
source: students
});
});
});
</script>
3: This is PHP (getStudents.php) file.
<?php
include_once "db_connect.php";
$sql = "SELECT * FROM student_reg";
$res = mysql_query($sql);
if($res){
$students = array();
while($r = mysql_fetch_array($res)){
$students[] = $r['sname'];
}
echo json_encode($students);
}else{
echo mysql_error();
}
?>
Note: when I test/ echo on the php file its self it show all names, but when I place cursor on "name" text field and type it doesn't show drop down suggested names as I prefer.