Jump to content

PHP doesn't output error from mysql query for non-existent rows for autocomplete


kkmoslehpour

Recommended Posts

I have a form that currently is able to auto complete base on user input, it queries the MySQL database and successfully lists all possible matches in the table and give suggestions. Now I want to handle rows that do not exist (when user enters a name that does not exist in the table). I am having trouble to get my PHP file to echo the error.  

 

Does anyone know why? Is it cause i am using GET?

 

 

Here is what I have so far:

 

search.php




    <?php
    //database configuration
    $host = 'user';
    $username = 'user';
    $password = 'pwd';
    $name = 'name';
    
     //connect with the database
    $dbConnection = new mysqli($host,$username,$password,$name);
    


    if(isset($_GET['term'])){
                //get search term
        $searchTerm = '%'.$_GET['term'].'%';


        //get matched data from skills table
        if($query = $dbConnection->prepare("SELECT * FROM nametbl WHERE name LIKE ? ORDER BY name ASC")) {


            $query->bind_param("s", $searchTerm);
            $query->execute();
            $result = $query->get_result();


            //$row_cnt = $result->num_rows;
            //echo $row_cnt;
            if($result -> num_rows){
                while ($row = $result->fetch_assoc()) {
                    $data[] = $row['name'];
                }


                //return json data
                echo json_encode($data);
                mysqli_close($dbConnection);
            }
            else { echo '<pre>' . "there are no rows." . '</pre>'; }
        }
        else {
            echo '<pre>' . "something went wrong when trying to connect to the database." . '</pre>';
        }
       
          
    }
    ?>


main.php

 

  


  <div id="gatewayInput">
    <form method="post">
          <input type="text" id="name" name="name" placeholder="Name..."><br><br>
          <?php 
            include("search.php"); 
          ?>    
    </div>


    ...
    ...
    ...


    <script src ="../../../jqueryDir/jquery-3.2.1.min.js"></script>
    <script src ="../../../jqueryDir/jquery-ui.min.js"></script>
    <script type="text/javascript">


    //auto search function
    $(function() {
          $( "#name" ).autocomplete({
              source: 'search.php'
          });
    });

Link to comment
Share on other sites

First of all, you don't need to include('search.php') in your file. autocomplete makes an HTTP request to 'search.php?term=xxx' and expects a JSON object back.

 

Source: When a string is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data.

If you're finding no rows, you're not returning a JSON object. So autocomplete doesn't know what to do with the data.

 

You could return a json object like echo json_encode(array('No data'));, but that's going to make it an option the user can select.

 

You may want to work out some different logic here. If nothing is returned, unhide a "no matches" span next to the box or something.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.