0

I try to create a live search using php mysql ajax

Input field

<input type="text" onkeyup="getProducts(this.value)" class="form-control">

Display result of search

 <div id="results"></div>

Ajax googleapis connection

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

Fuction getProducts

 <script type="text/javascript">
        function getProducts(value) {
            $.post("getProducts.php", {
                    partialState: value
                }, function(data) $("#results").html(data);
            });

        }

    </script>

getProducts.php code page

include("includes/connect.php");

$partialStates=$_POST['partialState'];

$states=mysqli_query($conn,"
        select 
        p.db_pcode,
        p.db_pname,
        p.db_punit,
        p.db_pprice,
        p.db_pqtyalert,
        p.db_pcategory,
        p.db_psupplier,
        p.db_pdesc,
        p.db_pvat,
        p.db_pdate,
        p.db_corid,
        c.db_ccat as categoryname,
        su.db_sname as suppliername,
        s.db_secname as blockname,
        s1.db_secid as cornerid,
        s1.db_secname as cornername,
        s2.db_secid as sectionid,
        s2.db_secname as sectionname
        from tbl_products as p
        left join tbl_category as c
        on 
        c.db_cid=p.db_pcategory
        left join tbl_suppliers as su
        on
        su.db_sid=p.db_psupplier
        left join tbl_section as s
        on
        s.db_secid=p.db_corid
        join tbl_section as s1
        on 
        s.db_parent=s1.db_secid 
        join tbl_section as s2
        on
        s1.db_parent=s2.db_secid 
        where 
        p.db_pname like '%$partialStates%'
        ")or die(mysqli_error($conn));
while($state=mysqli_fetch_array($states)){
    echo"<div>".$state['db_pname']."</div>";
}

The Problem is that the code don't display any thing

i try to open the getProducts.php page to see if i have any php error

but no error appear and the products are print

Can any one help to know where is the mistake ?? in the console i have this 2 errors

Uncaught SyntaxError: Unexpected identifier

2products.php:124 Uncaught ReferenceError: getProducts is not defined

jad
  • 37
  • 5
  • [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Apr 17 '17 at 17:59
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 17 '17 at 18:00
  • @JayBlanchard Thank you for your comment i have include the jquery library no error appear on the web browser – jad Apr 17 '17 at 18:05
  • @JayBlanchard if you mean the console i have this 2 errors Uncaught SyntaxError: Unexpected identifier 2products.php:124 Uncaught ReferenceError: getProducts is not defined – jad Apr 17 '17 at 18:10
  • There you go - you need to fix those errors. – Jay Blanchard Apr 17 '17 at 18:11
  • @JayBlanchard what this error mean please ?? – jad Apr 17 '17 at 18:12

1 Answers1

2

You have a syntax error ... missing a { in $.post callback

$.post("getProducts.php", {partialState: value}, function(data) { 
                                                            // ^^ missing
   $("#results").html(data);
 });
charlietfl
  • 170,828
  • 13
  • 121
  • 150