我需要此問題的幫助,請幫助我。如何在PHP中接收Ajax urlencode
我正在嘗試向PHP發送Ajax urlencode,但是當HTML直接發送到PHP時,PHP不會顯示POST內容。
我在Ajax中使用此代碼發送FormData到PHP。
有了這個簡單的PHP代碼,看看是否在PHP文件名的作品: 「thefile.php」
有了這個JS,HTML和PHP代碼:
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'];
?>
的輸入內容: 用戶:用戶名 通過:密碼
結果:
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
希望這有助於你。 [一](http://stackoverflow.com/questions/18906547/how-to-ajax-post-to-php),[two](http://stackoverflow.com/questions/9001526/send-array-with -ajax-to-php-script),[三](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php)。 –