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