我想在php頁面中顯示變量num
。將Ajax發送到php的變量undefined index
我正的誤差如下:
說明:未定義指數:NUM在C:\ XAMPP \ htdocs中\ javas.php第4行
PHP:
<?php
$lol = $_POST['num'];
echo "$lol " ;
?>
HTML/JavaScript的:
<html>
<head>
<script language="JavaScript" type="text/javascript">
function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "javas.php";
var num = "lol";
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(num); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
<input name="myBtn" type="submit" value="increment" onClick="javascript:ajax_post();">
<br /><br />
<div id="status"></div>
</body>
</html>
首先在你的php代碼中顯示你正在使用print_r($ _ post)的東西,其次糾正我,如果我錯了,我沒有看到你在這個頁面上發佈任何東西,因爲我沒有看到任何形式 –
你只需要表單元素爲ajax? – fosho
是你的PHP文件'javas.php'還是'my_parse_file.php'? –