gpt4 book ai didi

php - Ajax发送变量到php未定义索引

转载 作者:行者123 更新时间:2023-11-28 21:11:37 25 4
gpt4 key购买 nike

我想在 php 页面中显示变量 num

我收到的错误如下:

Notice: Undefined index: num in C:\xampp\htdocs\javas.php on line 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>

最佳答案

您必须发送名称/值对

hr.send("num="+ num);

关于php - Ajax发送变量到php未定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8479421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com