user3410277 user3410277 - 1 year ago 96
jQuery Question

Unable to send mails

I have an HTML form, and linked with jQuery and PHP. The jQuery check whether the fields or empty or not, if they are not empty, it should send the data to the PHP file. But its not working.

$(document).ready(function() {
$("#submit").click(function() {
var name = $("#name").val();
var message = $("#message").val();

$("#returnmessage").empty(); // To empty previous error/success message.
// Checking for blank fields.
if (name == '') {
alert(" Please Fill your name");
} else if (message == '') {
alert("Please Fill message");
} else {
$.post("xxx.php", { // To php file.
name: name,
message: message
}, function(data) {
if (data == "We will contact you soon.") {
$("#form")[0].reset(); // To reset form fields on success.

<form name="form" method="post" action="">
<p id="returnmessage"></p>
<label>Name: </label>
<input type="text" id="name" /><br>
<label>Message: </label>
<input type="text" id="message" /><br>
<input type="button" id="submit" value="Submit"/>


//Need to submit the form.
$name = $_POST['name'];
$message = $_POST['message'];

$email_body ="Dear sir,\n".
"Name: $name\n".
"Message: $message\n";

//Email sending to
$email_from = '';
$email_subject = "my-form";
$to = "xxx@xxxx";
$headers = "From: $email_from \r\n";

//Send the email!

Answer Source

You are using this check:

if (!isset($_POST['submit']))

And this would mostly never execute because of how you POST the data to the server and also, how each browser will take it. Especially Chrome, will not POST the submit. It mostly depends on the data that the AJAX is sending, so you need to see what's it sending:

Instead of your code, use:

if (count($_POST) == 0)

And also, make sure you have set up your mail() function to work with SMTP or which ever, in your php.ini. Only you could check your console, what the server response. So, give more details to your client like:

if (mail($to, $email_subject, $email_body, $headers))

The above part will help to diagnose where the script went wrong. At the same time, before exiting in the first part, respond with a message. Instead of your code, use:

if (count($_POST) == 0)
  die("Need to POST");

Also on the client side, make sure you use the latest or pretty decent version of jQuery (version > 1.9.1).

Looks like the OP needs more detailed explanation to find the bug. Could be helpful for others:

  1. Open your URL in Chrome. Please tell us what URL are you using in your address bar.
  2. Once the page is loaded, press F12 button. This brings up the Console.
  3. If that didn't work, right click and select Inspect, then click on Console. Make sure to clear console first by clicking on the Cross button.
  4. Reload the page. See if there are any Errors in Console.
  5. Tell us what do you see in the Console and after giving information, click on the Network tab, click submit, see if you have any activity.

Waiting for further responses.


You are checking in the success method:

if (data == "We will contact you soon.")

But where are you sending it in the PHP file? You need this at the end of the PHP file.

echo "We will contact you soon.";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download