2011-12-12 69 views
0

我想在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> 
+0

首先在你的php代碼中顯示你正在使用print_r($ _ post)的東西,其次糾正我,如果我錯了,我沒有看到你在這個頁面上發佈任何東西,因爲我沒有看到任何形式 –

+0

你只需要表單元素爲ajax? – fosho

+0

是你的PHP文件'javas.php'還是'my_parse_file.php'? –

回答

2

您必須發送姓名/值對

hr.send(「num =」+ num);

+1

是在發佈sa我的東西...必須有名稱/值對才能通過。 –

+0

這個回聲沒什麼 – fosho

+0

沒關係,謝謝,讓它開始工作 – fosho

0

此問題的一個部分是您發佈的參數未正確序列化。該參數字符串的POST類似於查詢字符串的GET請求:

var1=foo&var2=bar

既然這樣,現在你只需發送一個片原片數據在(賦給變量num