To check for a duplicate entry on our database, I have HTML:
<input class="form-control" type="text" id="items" name="items" value="{$items}">
<span class="duplicate" id="duplicate"></span>
The jQuery ajax script:
$(document).ready(function(){
$('#items').change(function(){
var items= $(this).val();
var Result = $('#duplicate');
if(items.length > 2) { // if greater than 2 (minimum 3)
var dataPass = 'a=check_items&items='+items+'&id_cat=id_cat';
$.ajax({
type : 'GET',
data : dataPass,
url : 'check.php',
success: function(responseText){ // Get the result
if(responseText == '1'){
Result.html('<span class="success">There is duplicate item.</span>');
}
else{
alert('Problem with query or php script.');
}
}
});
}
if(items.length == 0) {
Result.html('');
}
});
});
And check.php
if ('check_items' == $a && !empty($id_cat) && !empty($_GET['items'])) {
$get_items = strip_tags($_GET['items']);
$q_items = $DB->Prepare('
SELECT item
FROM tb_items
WHERE id_cat = ?
AND items = ?
LIMIT 1
');
$items = $DB->GetOne($q_items, array($id_cat, $get_items));
if (isset($items) || !empty($items)) {
echo '1';
} else {
echo '0';
}
exit();
}
I’ve already checked /check.php&a=check_items&items=Something&id_cat=1 , it printed ‘1’ (without single quotes), but there’s no change inside the result (id = ‘duplicate’).
Or if I change:
Result.html('<span class="success">There is duplicate item.</span>');
to:
alert('1')
the browser will display the alert value.
Any ideas?