I have this:
$("#upvote").click(function(){
var up = parseInt(document.getElementById('voteScore').innerHTML);
up++;
document.getElementById('voteScore').innerHTML = up;
$.ajax({
type: 'POST',
url: 'include/mysql_lib.php',
data: {'data[]':['upvote','<?php echo $id; ?>', '<?php echo $uid; ?>']},
dataType: "text",
success: function(dataType) {
if (dataType == "false") {
var up = parseInt(document.getElementById('voteScore').innerHTML);
up--;
document.getElementById('voteScore').innerHTML = up;
}
}
});
});
The mysql_lib.php file (if an error is found) has a line like this:
return "false";
What am I doing wrong? I’ve never used jQuery before.
The AJAX function’s server response (the ‘dataType’ variable in your code) stores whatever your PHP script wrote-out to the server. In your PHP script if you
return "false"that will return a string from a function, but if you want to pickup the value in your JavaScript you should useecho "false"so that the JavaScript’s response from the server will befalse.When running into issues like this it is a good idea to put a
console.log(dataType)oralert(dataType)inside your AJAX callback function to see what is being output by your PHP. The response from your PHP script can also be viewed in most developer tools (like FireBug).And a suggestion for ya. If you want to get into outputting more complex information from your PHP script, take a look at the PHP json_encode() function which makes communicating between PHP and JavaScript painless.