Ajax Jquery is not working in php page. I want retrieve mysql database in same php page without refresh the page. By selecting option from data list other text box will fill depend on list. Please help me to find the error.Thank you.
here is my javascript code
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#item").change(function(){
//alert("Changed");
$.ajax({
type:"POST",
url:"json.php",
data:{item:$("#item").val()},
success: function(data){
var iobj = $.parseJSON(data);
$.each(iobj, function(){
$("#item").val(this['hsn']);
$("#pack").val(this['pack']);
});
}
});
});
});
</script>
php file
<td valign="middle">
<input list="code" name="item" id="item" class="textbox" tabindex="8">
<td valign="middle">
<?php
$item=mysqli_query($con,"SELECT itname,itemcode FROM itementry");
echo '<datalist id="code" width="150%" style="font-size:30px;">';
while($row=mysqli_fetch_array($item)){
$itm = $row['itname'] .'|' .$row['itemcode'];
$ITMMM = $row['itemcode'];
echo '<optgroup>';
echo '<option value="'.$ITMMM.'" style="font-size: 50px;">';
echo '</option>';
echo '</optgroup>';
}
echo '</datalist>';
?>
</td>
<td valign="middle"><input type="text" name="hsn" id="hsn" class="textbox" tabindex="9"></td>
<td valign="middle"><input type="text" name="pack" id="pack" class="textbox" tabindex="10"></td>
<td valign="middle"><input type="text" name="rack" id="rack" class="textbox" tabindex="11"></td>
<td valign="middle"><input type="text" name="punit" id="punit" class="textbox" tabindex="12"></td>
json.php
<?php
include 'connect.php';
$item = $_POST['item'];
$start = strpos($item,'|')+2;
$code = substr($item, $start);
echo 'Item code:'.$item;
$sql = "select * from itementry where itemcode ='".$item."'";
$result = mysqli_query($con, $sql)or die(mysqli_error($con));
$rows = array();
while ($r = mysqli_fetch_assoc($result)){
$rows[]=$r;
}
echo json_encode($rows);
?>