1

I had a form that sent an email via ajax, which had been running smoothly for years. Suddenly, nothing is posted, and I do not understand why.

THE JS:

var name = $("input#name").val();
var email = $("input#email").val(); 
var telephone = $("input#telephone").val();
var message = $("textarea#message").val(); 

var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&message=' + message;  

$.ajax({  
  type: "POST",  
  url: "process.php",  
  data: dataString,  
}  
}); 

Process.php:

<?php
  if ($_POST) {
    $name = $_POST['name'];
    echo : $name;
  }

  else {
    echo 'Nothing is posted'; 
  }
?>

Everytime i use the form, the ajax "works", but "Nothing is posted" appears. I can't find an explanation... For information, I use Jquery 3.1.1, and PHP 7.0

sheriffderek
  • 8,217
  • 6
  • 38
  • 64
Damien
  • 323
  • 1
  • 3
  • 17
  • 2
    To start, you have an errant semi colon in your ajax declaration... Past that, are you sure you're actually posting anything? Perhaps try a `console.log(name);` before the ajax. Also, you could check the xhr request headers in your browser dev tools. – mituw16 Mar 21 '17 at 17:41
  • problem is you are using POST request but trying to pass query string in data. that will not work. If you are using POST in php end, it has to be parameterized json object in data attribute. – serdar.sanri Mar 21 '17 at 17:43
  • @Guyfawkes. You can send string data (url encoded in this case) over post requests. – mituw16 Mar 21 '17 at 17:48
  • @mituw16 but not as a POST request afaik.. – serdar.sanri Mar 21 '17 at 17:49
  • @guyfawkes I am trying to find documentation, but you an absolutely send a url encoded string as the body payload in a post request. http://stackoverflow.com/a/14551320/1729859 – mituw16 Mar 21 '17 at 17:52

3 Answers3

0

the correct way of implementing jQuery ajax is:

var name = $("input#name").val();
var email = $("input#email").val(); 
var telephone = $("input#telephone").val();
var message = $("textarea#message").val(); 
$.ajax({  
type: "POST",  
url: "process.php",  
data:{
 name:name,
 email:email,
 phone:phone,
 message:message
},  
}); 

And the php should be:

<?php
  if ($_POST) {
    $name = $_POST['name'];
    echo $name;
  }

  else {
    echo 'Nothing is posted'; 
  }
?>
Shakti Phartiyal
  • 5,370
  • 2
  • 19
  • 42
  • I don't know why they downvoted this, but it is the correct way to do it and should be accepted answer. He is using $_POST in the backend so data has to be passed by json object in data attribute – serdar.sanri Mar 21 '17 at 17:45
  • @guyfawkes exactly.. I use this approach and it works. This is what w3schools.com shows in their tutorial as well as the jQuery documentation. – Shakti Phartiyal Mar 21 '17 at 17:48
  • 1
    This doesn't really answer the original question of why the php script is printing *Nothing is posted* ... Changing from a url encoded post body to a data object body doesn't change anything – mituw16 Mar 21 '17 at 17:55
  • @mituw16 its an interesting question. Can you please answer this question. What according to you might be wrong? – Shakti Phartiyal Mar 21 '17 at 17:56
  • 1
    @ShaktiPhartiyal I do not know what is wrong. That is why I asked OP to check the data he is posting, and also to check with his browser's dev tools to monitor the request – mituw16 Mar 21 '17 at 17:59
  • I check my data before the post, and everything is ok. The problem is not in the data or the way they are declared in the ajax. The problem it that it seems that nothing is posted. Nothing at all. – Damien Mar 22 '17 at 14:41
0

jQuery changed type to method in version 1.9. Try changing the ajax call to this:

$.ajax({  
  method: "POST",  
  url: "process.php",  
  data: ...
});
Adam Mazzarella
  • 768
  • 1
  • 9
  • 14
0

Try this:

    if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
      // POST
    } else {
      // GET
    }
  • Thanks, but in doesn't change anything. – Damien Mar 22 '17 at 14:42
  • if that doesn't work, then I guess the way you are sending the data is not correct. In place for sending the data as a query string, try serializing the data in this way: `data: $("#idForm").serialize()` If serialize doesn't work then create a new object using the from data and then send that. – Sunil Jhamnani Mar 26 '17 at 09:19