Hi, I was trying to do this on my local wamp server...but to no avail...and getting some kind of error but it was in html code, when using "alert"..but after putting into a tag, it displays a tabel with the error message....
just to clarify, the error is no longer a problem, I tested it on the actual server and it emailed just fine...but my question is...if my php code only had two echos...one for positive sending "success" and for an error "error"...then why I am I getting all this other junk in the result?
The image of the result in the alert box is this: (code follows image):
Your $_POST was being checked against first, which would always have come true in the event that your form was correctly submitted.
This makes no sense. It'll work exactly the same with or without your terminated if-condition I'm not sure if you typed the semi-colon by accident, but your "Success" line is not part of your if-statement, it's going to get executed no matter what the mail function returns:
This would then have caused your mail function to display a basic error on-page, but not the actual fail code of the function itself.
I'm not familiar with jQuery, but I can see you're using AJAX calls from JavaScript. I am familiar with error messages showing up in alert dialogs because I'm a fan of AJAX myself, but I use raw JavaScript and not jQuery.
The reason for this is your HTTP response text (the data sent back to JavaScript from PHP) happens to be the error from the mail function, which would otherwise have simply been printed out in the browser if you weren't using AJAX. This is one of the annoying things about AJAX, it's hard to debug because things don't always work as you expect them to.
Thanks for explaining that.... It's nice to know someone knows how it works...lol.
So, how would I actually be able to override that message with my custom message? . ...so pretty much if it fails, I have a variable with the"error"in it.. I don't need to know the error, but read the echoed result from the php file...and then I can compare it ..if it says error display an error message .if success then a success message. .thanks for the help
If you want to override the error reporting on a particular function, you will have to slightly alter your code. You can use the "at" operator to suppress error messages, but only when using an expression. The PHP.net site has a description: http://php.net/manual/en/language.op...rorcontrol.php
Their rule of thumb is that if the construct of your choice is able to have its return value stored, you can suppress the errors it will produce.
This means you would need to store your mail function return value in a variable like so:
I don't know for sure if this will actually work for the function in question, but I'm confident it should. There are always other options, like suppressing error reporting all together, which I don't recommend.
Anyway, I don't know what your requirements are so I can't say whether this code will suffice, but I expect it to work as intended.
by the way...I can't remember off the top of my head...how do you actually get the result from the php though? I mean if it echos for example "success"...how from javascript or even jquery, do I recieve that info?
As I said I don't use jQuery so I don't fully understand how your code is working. I'm assuing you've copied & pasted it since you don't know yourself.
However, in raw JavaScript you would receive back the responseText object. The responseText object is a string object which contains within it all the data which was echoed from the PHP file. In essence, anything outputted by the PHP file can be considered responseText.
If you can figure out for yourself where your responseText is being handled, you can store it in a variable, whereupon you will be able to check it for a string.
I can only give you JavaScript code, so if you don't understand the concept of what I'm explaining, I won't be able to help you any further.
PHP Code:
// JavaScript code
var xmlhttp = "";
var get_php = "";
if (window.XMLHttpRequest) // IE7+, Firefox, Chrome, Opera, Safari.
xmlhttp = new XMLHttpRequest();
else // IE6, IE5.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
// The "get_php" variable contains all data outputted by PHP.
get_php = xmlhttp.responseText;
alert(get_php);
}
}
xmlhttp.open("GET", "ajax_php.php", true);
xmlhttp.send();
From the looks of your code, I'd say the raw JavaScript method for AJAX is actually simpler and easier than the JQuery method. If you're interested, I recently wrote an article on how to retrieve PHP data through JavaScript on my new web site. The code is very simple; I'd say much simpler than yours despite the jQuery.
As for your question of how to tell in JavaScript whether or not a success or failure was outputted by PHP, that can be achieved with a simple match() statement or perhaps a very basic regular expression. Something like this should work:
PHP Code:
// JavaScript code
// You'll have to fill this line in yourself, I don't know the jQuery.
var your_response_variable = some_response_text;
// Search for success or failure.
if(your_response_variable.match(/success/i))
{
alert("Success");
}
else if(your_response_variable.match(/failure/i))
{
alert("Failure");
}
else
{
alert("Unknown response");
}
Inside the match function I am using a very basic regular expression. Anything inside the // will be the string checked for. The "i" tells the expression to ignore case.
Hope that helps; I recommend using JavaScript over jQuery but if you're able to get it working, that's fine.
Bookmarks