I am trying to make a name tagger in jquery + php. I have done the PHP part and jquery code is 99% done, but my problem is passing the string to search for after the ‘@’ sign. I currently use a div with contentEditable set to true. From what I see in console log the name variable is indeed @+search string, but the list div will not be populated, I tried running the script manually just to debug and it works, including setting the name variable to one specific letter and it populates the list div. Any tips and suggestions appreciated my code below:
$("#chat-message").bind("keyup", function (event) {
//auto name complete
var search = $(this).text();
var searchbox = search.split(search.indexOf('@') + 1);
var checker = search.substr(search.length - 1); //Checks last charcter inserted if its '@'
var name = search.match(/@(\w+)/ig);
console.log(name);
var dataString = 'searchword=' + name + '&search=yes';
if (name == '' || checker == " ") {
$("#display-search").hide();
} else if (checker === "@") {
$.ajax({
type: "POST",
url: "searchy.php",
data: dataString,
cache: false,
success: function (html) {
$("#display-search").html(html).show();
}
});
}
return false;
});
Try changing your
ajaxcall to this:What, if anything, do you see in the console?
UPDATE
If you’re not seeing anything, AJAX isn’t getting called. Try a console log to make sure
checkeractually equals'@'.UPDATE:
The fundamental problem you’re going to have with your logic is that the AJAX function will only be called when
checker === '@'. Ascheckeris the last character that the user typed,namewill never exist whenchecker === '@'.I would suggest the following logic instead: