-3
嗨即時嘗試訪問ajax函數中的php變量,但顯然它不工作......我已經使用onClick事件來激活ajax函數,我通過我的本地PHP變量作爲參數參數...php變量爲ajax函數
<?php
$name = $_GET['name'];
?>
<html>
<head>
<script language="JavaScript" type="text/javascript">
function ajax_post(x){
var nm = x;
var hr = new XMLHttpRequest();
var url = "my_parse_file.php";
var fn = document.getElementById("first_name").value;
var vars = "todo="+fn+"&name="+nm;
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
}
}
hr.send(vars); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<?php
$display =' Name of list:;
echo <label for="name"></label>
<input type="text" name="name" id="name">
</p>
<p>Name of item:
<input id="first_name" name="first_name" type="text" />
<br /><br />
<input name="myBtn" type="submit" value="Submit Data" onClick="javascript:ajax_post(' . $name . ');">
</p>
<p>Your list has been succesfully created.</p>
<form name="form1" method="post" action="">
<input type="submit" name="AddItem" id="AddItem" value="Add Items">
</form>
<p><br />
<br />
</p>
<div id="status"></div>
</body>
</html>';
?>
<?php
echo $display;
?>
您的問題是?什麼是錯誤? – 2012-02-29 22:50:42
如果我每次出現這個問題都有鎳,我可以將它們全部熔化並建立Voltron。 PHP是服務器端,Javascript是客戶端。他們不分享變數。將PHP變量放入一個Javascript變量的唯一方法是明確地將它放在那裏:var name ='<?php echo $ name; ?>';'將一個Javascript變量放入PHP變量的唯一方法是通過GET或POST請求將其發送到服務器。 – 2012-02-29 22:53:13
@Justinᚅᚔᚈᚄᚒᚔ - 謝謝我以前曾嘗試使用過的方法,但是我的語法錯了... – higfox 2012-02-29 22:59:33