0

I have a JS code as below :

function validateForm(){


 // some code 
 $.get("../sendMail.php");

 alert('Reached here ? then the mail was sent successfully');
 // more stuff

}

and the sendMail.php code :

<?php
$to = "[email protected]";
$subject = "MY PHP MESSAGE";

$name = $_REQUEST['myName"'];
$phone = $_REQUEST['myPhone'];
$email = $_REQUEST['myEmail'];

$message .= "<br>"."<br>";

$message .= "<strong><font color='red'>Information Below.</font></strong>"."<br>"."<br>";

$message .= "<strong>Name:</strong> ".$name ."<br/>";
$message .="<strong>Phone:</strong> ".$phone."<br/>";
$message .="<strong>Email:</strong> ".$email."<br/>";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: [email protected]' . "\r\n";

mail($to,$subject,$message,$headers);   
?>

Even though the alert in the JS works great , the mail is not sent .

Any idea what's wrong with the code ?

Much appreciated

(FYI , I'm running on the localhost , if it makes any difference)

EDIT :

$.ajax({
    url: '../sendMail.php',
    success: function(data, textStatus, jqXHR){
        alert(console.log('data: %O', data));
        alert(console.log('textStatus: %s', textStatus));
        alert(console.log('jqXHR: %O', jqXHR));
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert(console.log('jqXHR: %O', jqXHR));
        alert(console.log('textStatus: %s', textStatus));
        alert(console.log('errorThrown: %s', errorThrown));
    }});

but nothing was sent , and nothing is being printed on the screen . No alert , nothing .

3
  • 1
    You didn't send any parameters in your $.get call. Commented Feb 7, 2014 at 16:47
  • Make sure you have a local mail server, because PHP's mail() function sends with the current machine's mail server... Commented Feb 7, 2014 at 16:51
  • Your script isn't checking whether mail() was successful. Check the Network tab of Developer Tools to see if the script reported any errors. Commented Feb 7, 2014 at 16:53

3 Answers 3

3

jQuery's .get method is asynchronous. Meaning that the alert will appear whether the AJAX call is successful or not. What you need is this:

$.get('../sendMail.php', function(data){
    console.log('Reached here ? then the mail was sent successfully');
}):

Incidentally, .get is a convenience alias for .ajax. If you're having trouble, you should use .ajax, which gives you more options for debugging:

$.ajax({
    url: '../sendMail.php',
    success: function(data, textStatus, jqXHR){
        console.log('data: %O', data);
        console.log('textStatus: %s', textStatus);
        console.log('jqXHR: %O', jqXHR);
    },
    error: function(jqXHR, textStatus, errorThrown){
        console.log('jqXHR: %O', jqXHR);
        console.log('textStatus: %s', textStatus);
        console.log('errorThrown: %s', errorThrown);
    }
});

See the documentation for .ajax for all the options available to you.

Sign up to request clarification or add additional context in comments.

7 Comments

See my edit. I gave you some avenues for collecting extra information.
Add the code I suggested, then report back with what you see on the log. It will help us fix the problem.
Are you looking at the console log? I'm just not sure what you mean when you say "nothing is printed to the screen". There should be something....
I added another alert after the code you posted , and the alert works , but the code before that (your code) is not invoked ... I guess . I tried both with console and alert , but your code is not invoked .
Hm. Something strange is definitely going on. One of success or error should have been called. Can you put something in your PHP code so you know that you're getting there? Even a file_put_contents("C:\\temp\\test.txt", "mail function called"); would suffice....
|
0

You are also missing the parameters to send to the PHP file, it is waiting to receive:

$name = $_REQUEST['myName"'];
$phone = $_REQUEST['myPhone'];
$email = $_REQUEST['myEmail'];

So you should also send them on the request:

$.ajax({
  type: "POST",
  url: "../sendMail.php",
  data: { myName: "John Doe", myPhone: "123456789", myEmail: "[email protected]" }
}).done(function( msg ) {
   alert( "Mail sent: " + msg );
});

I suggest you to learn about the AJAX request and how jQuery does it:

Also refer on how PHP handles the $_REQUEST var:

Greets!

Comments

-1

Use this one,

var myName = $('#name').val(); //get from input fields
var myEmail = $('#email').val();
var myPhone = $('#phone').val();
$.ajax({
  url: "../sendMail.php", //url to send
  type:"get", //get or post
  data: "myName="+myName+"&myEmail="+myEmail+"&myPhone="+myPhone, //data to be send
  success:function(response){ //on success
    alert('Reached here ? then the mail was sent successfully');
  },
  error: function(response){ //on error
    alert(response);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.