隨着jQuery的:
<?php
if (isset($_POST['data'])) {
$data = $_POST['data'];
print("data is: $data");
return;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery-1.6.3.js"></script>
</head>
<body>
<div>Response from server: <span id="response"></span></div>
<script type="text/javascript">
$.post('test.php',{'data': "value1"}, function (data) {
$('#response').text(data);
});
</script>
</body>
</html>
非jQuery的 IE沒有XMLHttpRequest對象所以這裏是IE墊片:
Yes, using ajax/javascript:
var formData = new FormData();
formData.append("input1", "value1");
var xhr = new XMLHttpRequest();
xhr.open("POST", "page1.php");
xhr.send(formData);
if (typeof XMLHttpRequest == "undefined") XMLHttpRequest = function() {
try {
return new ActiveXObject("Msxml2.XMLHTTP.6.0");
}
catch (e) {}
try {
return new ActiveXObject("Msxml2.XMLHTTP.3.0");
}
catch (e) {}
try {
return new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
//Microsoft.XMLHTTP points to Msxml2.XMLHTTP and is redundant
throw new Error("This browser does not support XMLHttpRequest.");
};
來源
2011-09-14 01:31:28
Joe
我喜歡jQuery解決方案,但我有點困惑。那一行代碼是唯一需要的嗎?它到底在哪裏? php變量是如何分配的? – user617123
是的,用jQuery你只需要一行。在你的PHP中,你會像$ _POST ['input1'] – Joe
那樣訪問它,嗯,我一定是做錯了。在page1.php的頭部我有''然後在body中我有'
\ n \ n <?php \ n $ data = $ _POST ['input1']; \ nprint(「value is:$ data」); \ n?>'我得到一個空白($數據是空的)。 – user617123