I am using JQUERY .ajax() to send serialized form data to MySQL using PHP. The backend PHP being called will return a message if there was an issue processing the data. My Javascript function then checks for a message being returned before deciding what to do next. If the message length is ‘0’, then the post was successful and it can carry on. If the message length > ‘0’, then an error must have occured in the form processing, and it traps there to deal with it. However, when testing, I found that a successful processing of my form data, returned a message of size 13, even though the message itself was blank.
The javascript function I used is below. I have modified it to test for a message length of more than 13 characters, and this is working fine now. I’ am just curious as to why a blank response is 13 characters in length. Ignore the complicated .load() statements, they just return the user to a different page depending on whether an error message was detected or not.
$("button#update").click(function() {
var cat = $("select#cat_select").val();
var dataString = $("form#project_update").serialize();
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
dataType: 'text',
success: function( msg ) {
console.log('msg is ' + msg);
length = msg.length;
console.log('length is ' + length);
if (length > 13) { // no idea why a blank msg has a length of 13!!
alert(msg);
$("#main").load('?app=$app&func=$func&sub=ajax&ajaxSub=edit_project&cat=' + cat + '&proj_id=' + $proj_id).fadeIn('fast');
}
else {
$("#main").load('?app=$app&func=$func&sub=ajax&ajaxSub=project_detail&cat=' + cat + '&proj_id=' + $proj_id).fadeIn('fast');
}
}
});
return false;
});
Instead of
try with
Note
But you should make sure that there are no spaces or newlines within
<?phpand?>tags, I would be better if you remove them.