What I'm trying to do with this is if number of rows selected is greater than or equal to 1 echo this json_encode(array("item" => $item));
, or else if number selected rows is equal to zero echo this echo json_encode(array("failed" => 'failed'));
. But this isn't working. The output of this was there's no post value returning even the record selected was greater to one. Any help?
Php
<?php
if(isset($_POST['id'])) {
$id= $_POST['id'];
$sql = $mysqli->query("SELECT item FROM table WHERE id='$id'");
while($row = $sql->fetch_assoc())
{
$item= $row['item'];
}
if(($sql->num_rows)>= 1){
echo json_encode(array("item" => $item));
} else {
echo json_encode(array("failed" => 'failed'));
}
}
?>
Ajax
$.ajax({
type: "POST",
url: "autocomplete-ajax.php",
data :"id="+id,
dataType:'json',
type:'POST',
success: function(msg){
console.log(msg)
var item_ajax=data.item;
if(msg == 'failed') {
$('#item').val('');
} else {
//send to element ID
$('#item').val(item_ajax);
}
}