0

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.

markpsmith
  • 4,860
  • 2
  • 33
  • 62
mcl
  • 71
  • 8
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Nov 20 '15 at 14:00
  • 1
    Are you by any chance confusing the two textboxes "name" and "idnum"? – Culme Nov 20 '15 at 14:04

1 Answers1

0

Just an idea, but I would suggest you check out Datatables (https://www.datatables.net/). It might have some very useful applications for your app. The search functions do pretty near what you wish I think. Here's a very basic example with the searching functions available.(https://www.datatables.net/examples/basic_init/zero_configuration.html).