1

I need help with this problem, please help me.

I'm trying to do Ajax urlencode to PHP, but PHP doesn't show me the POST content like does when HTML send directly to PHP.

I'm using this code in Ajax to send FormData to PHP.

With this simple PHP code to see if works on php file name: "thefile.php"

With this JS, HTML and PHP code:

function sendme() {
  var form = new FormData(document.forms['form']);
  if (window.XMLHttpRequest)
    var ajax = new XMLHttpRequest();
  else
    var ajax = new ActiveXObject("Microsoft.XMLHTTP");
  ajax.open("post", "thefile.php", true);
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
  ajax.onreadystatechange = function() {
    if (ajax.readyState == 4 && ajax.status == 200)
      console.log(ajax.responseText); //to see the return on in console
  };
  ajax.send(form);
};
<form name="form" onsubmit="return false;">
  <input type="text" name="user" required autofocus/>
  <input type="password" name="pass" required/>
  <input type="submit" name="send" onclick="sendme();" />
</form>
<?php
    print_r($_POST); //to see $_POST Array Content
    echo ' '.$_POST['user'].' '.$_POST['pass'];
?>

The input content: user: username pass: password

The results:

Array
(
[------WebKitFormBoundary50040KVnXutLwSAd
Content-Disposition:_form-data;_name]=>"user"

username
[------WebKitFormBoundary50040KVnXutLwSAd
Content-Disposition:_form-data; name]=>"pass"

password
------WebKitFormBoundary50040KVnXutLwSAd--
)

Notice:  Undefined index: user in thefile.php on line 3
Notice:  Undefined index: pass in thefile.php on line 3
  • Hope this help you. [One](http://stackoverflow.com/questions/18906547/how-to-ajax-post-to-php), [two](http://stackoverflow.com/questions/9001526/send-array-with-ajax-to-php-script), [three](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php). – Danila Ganchar Sep 05 '15 at 07:51

2 Answers2

1

From the specification:

FormData: Push the result of running the multipart/form-data encoding algorithm, with object as form data set and with utf-8 as the explicit character encoding, to stream.

You are generating a multipart/form-data body, but you are explicitly setting the content-type header to claim that it is application/x-www-form-urlencoded; charset=UTF-8.

Either:

  • Specify the correct content type
  • Don't specify the content type and let XHR set it for you
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

It seems like your httpHeader "application/x-www-form-urlencoded" is not compatible with FormData object.

Just comment it out like below:

ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");

to

//ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");

If you prefer to use "application/x-www-form-urlencoded; charset=UTF-8" in your request. You have to write simple javascript code to make it as normal string like:

var form = "user=root&pass=root";
.....
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
.....
ajax.send(form);