-1

This Ajax call triggers during change event because it alerts the $name1 variable.

But the name1 variable is not passing to the data.php file.
data.php independently works correctly.

How do I correct that? How to correctly pass the name1 variable?

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$("#name").on('change',function (e){
  var name1 = this.value;
  // alert("name1 = " + name1);
  $.ajax ({
    data:{name1: name1},
    type: 'POST',
    url: 'data.php',
    success: function (response){
      console.log(response);

      $('.products-wrp').html('');
      $('.products-wrp').hide();
      $('.products-wrp').html(response);
      $('.products-wrp').show();
    }
    error: function (){
                alert('No data found!');
            }
});

});

data.php

<?php
$name1 = $_POST['name1'];
$results = $mysqli_conn->query("SELECT product_name, product_desc, product_code, product_image, product_price FROM products_list where product_name='$name1'");
$products_list =  '<ul id ="products_list" class="products-wrp">';
while($row = $results->fetch_assoc()){
  $products_list .= <<<EOT
  <li>
    <form class="form-item">
      <h4>{$row["product_name"]}</h4>
      <div>
        <img src="images/{$row["product_image"]}" height="62" width="62">
      </div>
      <div>Price :{$currency}{$row["product_price"]}<div>
    </form>
  </li>
  EOT;
}
$products_list .= '</ul></div>';
echo $products_list;
?>
Lilly Meow
  • 45
  • 6
  • console log name1 and verify whether you are getting name1 value in javascript before triggering ajax. is data.php and the html code in same location? – divine Jun 03 '17 at 06:19
  • here you have write $name1 in '$name1' , so you won't get it's value inside the query – Meera Tank Jun 03 '17 at 06:22
  • 3
    Try to use the error function of AJAX, and tell us what you get – Jose Marques Jun 03 '17 at 06:32
  • 1
    @Lilly Meow: Some [reading](https://softwareengineering.stackexchange.com/questions/171203/what-are-the-differences-between-server-side-and-client-side-programming)? *( 9 questions in 5 days... Worths good reading for the week-end. ;) )* – Louys Patrice Bessette Jun 03 '17 at 06:45
  • Try to check what is being transmitted by your post. You have two ways to see this through the console of your browser or by creating an error_log in your php to check the value $name1. Tell us what you get. – Jose Marques Jun 03 '17 at 06:55
  • And for getting better in the use of ajax you can get the type of error, give a look at this link https://stackoverflow.com/questions/6792878/jquery-ajax-error-function – Jose Marques Jun 03 '17 at 07:05
  • @JoseMarques i dont het name1 in my error console.. so hoping that its passing inside the name1 query – Lilly Meow Jun 03 '17 at 07:46
  • What I said is to perform debugging and not for you to implement in your project. In your php code, place "error_log ($name1);" and then, in the console of your server verify if the data you send its the one you receive. This is because sometimes AJAX sends data in a way the server does not understand, and the value is given as undefined. – Jose Marques Jun 03 '17 at 12:20

1 Answers1

0

$results = $mysqli_conn->query("SELECT product_name, product_desc, product_code, product_image, product_price FROM products_list where product_name='$name1'");

You got: product_name='$name1'. Shouldn't it be: product_name='{$name1}'

marconato
  • 51
  • 7