elAhmo Posted February 8, 2010 Share Posted February 8, 2010 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. Quote Link to comment Share on other sites More sharing options...
trq Posted February 8, 2010 Share Posted February 8, 2010 if (!PEAR::isError($mail)) { echo "0"; } else { echo"1"; } Quote Link to comment Share on other sites More sharing options...
elAhmo Posted February 8, 2010 Author Share Posted February 8, 2010 Hah, that works. Sounds too simple to be true, but it works Thanks. And how does that AJAX code "know" how to check, or wait, for output of that specific part of php code? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.