0

when a form is submitted regularly via POST the contents of the values of the form are stored in $_POST but then the jquery .ajax function is used on an .submit what is the variable used to retrieve the data in the php file that processes the form?

waa1990
  • 2,365
  • 9
  • 27
  • 33
  • 2
    can you show your relevant jquery code? – Daniel A. White Dec 16 '11 at 19:07
  • possible duplicate of [Issue reading HTTP request body from a JSON POST in PHP](http://stackoverflow.com/questions/7047870/issue-reading-http-request-body-from-a-json-post-in-php) – mario Dec 16 '11 at 19:08
  • If you are submitting to a PHP page via `.ajax` POST, the variables are retrieved as $_POST. – jk. Dec 16 '11 at 19:09
  • @mario I don't think the OP is trying to read the request body. Sounds like form variables - so not a duplicate of the post you cited. – jk. Dec 16 '11 at 19:13
  • @jk: Well, maybe and no. Then it would be just a different duplicate. Like [Why can't I get my JSON Data posted using AJAX in my PHP file?](http://stackoverflow.com/questions/8285451/why-cant-i-get-my-json-data-posted-using-ajax-in-my-php-file) – mario Dec 16 '11 at 19:14

2 Answers2

3

The jQuery $.ajax function takes a type parameter in the options, which sets whether the request should be done via post or get. It defaults to get.

If it's set to get, you can get to the info in PHP via the $_GET superglobal. On the other hand, if you set it to post (which is probably what you want), you can get to it via $_POST.

Example:

$.ajax({
  url: 'ajax/test.html',
  type: 'post',
  data: {firstName: 'John', lastName: 'Doe' },
  success: function(data)
  {
    alert( 'We have data: ' + data.toString() );
  }
});
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
0

That depends on what you set for the type argument. The default is get.

$.ajax({
  type: "post",   <---- type  
  url: "some.php",
  data: {somevar:'var'}
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});
aziz punjani
  • 25,586
  • 9
  • 47
  • 56