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;
?>