Jump to content

Checking output of PHP function with AJAX


elAhmo

Recommended Posts

Hello.  Im new on this forum so Id like to thank you in advance for every piece of advice and  help you'll give to me in the future. Im a beginner, but I hope that I will be also able to help others very soon. :)

 

I've encountered one problem, and I've spent last two hours tryin to figure it out, and having in mind that it's 3am here, Ive decided to ask for help.

 

So, here is my problem. I have one form on my site, which makes POST req to mail.php with email. And mail.php handles mail sending. However, it seems that mail don't "pass" every time, so a simple check whether the PEAR (Im using PEAR Mail package to send mails with SMTP authorization) has returned an error or not would be cool. And that check goes like this:

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}

($mail is the function for sending mail)

 

This would be nice if I used directly mail.php, but since it's used though POST from ajax form, it doesn't give me any result.

 

Code of my form is:

<form action="mail.php" method="post" id="form-email">
				<p>
					<input type="text" name="email" id="email" value="Enter your email address."/>
				</p>
				<p>
					<input type="submit" name="submit" id="submit" value="" />
				</p>
			</form>

 

And the AJAX part goes like this:

	$("form#form-email").submit(function() {
	var email = $("input#email").val();
	$.ajax({
		url:'mail.php',
		type:'post',
		data: "email="+email,
		success: function(msg){
			if (msg==1)
				$("input#email").val('Everything went OK');								
			else
				$("input#email").val('An error occured');		
		}
	});
	return false;
});

(I found this AJAX code online)

 

 

So, my question is this: How can I make a check, from this AJAX "success part", to see what code in PHP has returned? Maybe I could define some variable or something like that in mail.php, so when PEAR returns an error, that variable is set to 0, else to 1, and a function from AJAX could simply check that variable and see it's value and return text. Or this thing with function(msg), maybe I can somehow define its value from mail.php code?

 

Thanks again for your time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.