The problem i have is with a simple ajax mail script.
I have it working fine like this:
php snippet:
if(mail($to,$subject,$message,$headers)){
echo "success";
}
And back in the ajax code its handled here :
if($.trim(data) == "success"){
// do the success stuff
}else{
// doo the fail stuff
}
My concern is that in the jquery code "trim" is not supported in all versions of explorer. So i would rather have the php return the string "success" already trimmed. But changing things to what you see below does not work as i expected, and runs the fail function in the ajax..?
Ammended php :
if(mail($to,$subject,$message,$headers)){
$success = trim("success");
echo $success;
}
Ammended Ajax :
if(data == "success"){
// do success stuff does not happen anymore
}else{
// now it always does fail stuff, so comparison is always false
}
Why is doing the trimming in the php instead of the ajax letting me down please..?
Your problem is not with the string "success". Trimming just removes spaces and white-characters (newlines, etc.) from the beginning and ending of a string. And in "success" there is no such thing, so it does nothing. However, if trimming on the JavaScript side works, then your PHP must be producing some whitespace characters, so it appears in the response. But this is not in the code you posted, there is no problem with the work "success". You have to go through your code and find the source of the newlines/spaces/white characters.
Usually, the problem is with Unicode BOM marks (look that up and open your PHP files in an editor that can show you if they are present or not) or newlines at the end of a PHP file (remove these).
If you use jquery.ajax you can specify success/complete/failure callback functions. Your jquery call could be a function that executes your mail function and then returns true or false. You can then check the return in your success function.
Just try this... in ajax
$('#err_register').html("Data:"+data+"Display");
var resultData=jQuery.trim(data);
if(resultData == "success"){
// do success stuff does not happen anymore
}else{
// now it always does fail stuff, so comparison is always false
}