I have a table: DB
city
-------
cityID
cityname
store
I have then a form HTML:
<input type="text" class="store">
GOAL:
I would like javascript, after I enter the store, (if the entered value is already in the DB) displays an alert like:
"Store is already entered for the following cities: New York (ID#), Boston(ID#), Springfield(ID#)"
I've tried with a Json file:
<?php include ('connectionlink.php');
$word = $_GET['word'];
$search = "SELECT
store as value,
cityID,
cityname,
FROM city
WHERE store LIKE '%".$word."%'";
$result = mysqli_query($connection, $search);
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
$row['value']=htmlentities(stripslashes($row['value']));
$row['cityID']=$row['cityID'];
$row['cityname']=$row['cityname'];
$row_set[] = $row;
}
echo json_encode($row_set);
?>
And javascript
$(document).ready(function (){
$('.store').on('change', function(){
var storeValue = $('.store').val();
$.post('stores.php',{'word' : storeValue}, function(data) {
alert("Data: " + data);
});
});
});
I feel I'm almost there, because after typing the store I get an alert of Undefined index word error and then the alert displays all the data in my table with the Json format. It's like if it doesn't search just for my word, but just returns everything. Thanks for your help!