-1

I'm trying to figure out how to send the results from an AJAX search but it does not work. I want to return the results back to the website after searching, I think my problem comes from the SQL query but I can't figure it out.

This is my php code:

<?php
include('db_conn.php');

$output="";

  $search = mysqli_real_escape_string($mysqli,$_POST["search_text"]);
  $query = "SELECT * FROM unit
  WHERE id LIKE '%$search%'
  OR unit_code LIKE '%$search%'
  OR unit_name LIKE '%$search%'
  OR lecturer LIKE '%$search%'
  OR semester LIKE '%$search%'
  ";
$result = mysqli_query($mysqli,$query);
if(mysqli_num_rows($result) > 0){
  $output .='<div class="table-responsive">
   <table class="table table bordered">
    <tr>
     <th>ID</th>
     <th>Unit Code</th>
     <th>Unit Name</th>
     <th>Lecturer</th>
     <th>Semester</th>
    </tr>
       ';
  while($row = mysqli_fetch_array($result)){
    $output.='
       <td>'.$row["id"].'</td>
       <td>'.$row["unit_code"].'</td>
       <td>'.$row["unit_name"].'</td>
       <td>'.$row["lecturer"].'</td>
       <td>'.$row["semester"].'</td>
    </tr>
    ';
  }
  echo $output;
}
else {
  echo 'Error php';
}

 ?>

And this is the ajax:

   <script type="text/javascript">
        $('#search_button').click(function(){
          var keywords = $('#search_text').val().trim();
          $.get("search.php",
                {keywords:keywords}).done(function(data){
                  alert('test');
                  $('#result').html(data);
                });
        });
       </script>
halfer
  • 19,471
  • 17
  • 87
  • 173
  • 1
    If you think the SQL is the problem, check for errors and see what is happening - https://stackoverflow.com/questions/17053466/how-to-display-errors-for-my-mysqli-query – Nigel Ren May 02 '20 at 07:09
  • You're sending a value of keywords with the key "keywords" as get-request, while you in the PHP are checking the $_POST-request of "search_text". You have to either do a post or a get and check the right key (in this case "keywords" instead of "search_text"). – bestprogrammerintheworld May 02 '20 at 07:21
  • 1
    Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then check the [help/on-topic] to see what questions you can ask. Please see: [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236). – Progman May 02 '20 at 07:44
  • Note that `mysqli_real_escape_string` will not protect you from SQL injection in all cases. Use parameter binding instead. – halfer May 02 '20 at 11:44

1 Answers1

0

I believe your problem is that you are using $.get, but you are attempting to retrieve the value in your PHP from the $_POST variable which would have nothing in it, it would be in the $_GET variable.

I suggest you change your ajax call to use $.ajax

<script type="text/javascript">
  $('#search_button').click(function(){
    var keywords = $('#search_text').val().trim();
    $.ajax({
      url: "search.php",
      data: {search_text: keywords},
      method: 'POST',
      success:function(data){
        $('#result').html(data);
      },
  });
</script>

The first key in the data array should match the key you use in PHP to collect the value from $_POST

Bertyterty
  • 106
  • 4
  • Can you help me to check out my output in search.php. When I run the search.php, it only displays the table head but I want to show the data from search results in that table. Thank you in advance. – Newbie2020 May 02 '20 at 08:34
  • Assuming that your database connection is working correctly, I think your problem is quite simple in search.php. mysqli_real_escape_string only accepts one argument, just remove $mysqli from that method leaving the $_POST variable and it should work. You can use var_dump($result) and check the network tab of your browser developer tools to check you are actually getting results from the database – Bertyterty May 02 '20 at 08:42