php - 如何将ajax变量值传递给php页面?
问题描述
我正在尝试使用 ajax 传递变量值。当我运行显示正确值“hi”的文件警报框时,但在 test.php 上出现错误
注意:未定义索引:第 19 行 C:\wamp64\www\abrar\superadmin\test.php 中的消息
我的代码是
superadminviewbtn.php
HTML
<script>
var message = "Hi";
$.ajax({
url: "test.php",
method: "post",
data: {
"message": message
},
success: function(data) {
alert(message);
},
error: function() {
alert('Not OKay');
}
});
</script>
测试.php
PHP
<?php
$msg = $_POST['message'];
echo $msg;
?>
但它显示错误test.php
解决方案
你必须纠正你的success: function
:
success: function(data) {
/* data is the return string form the PHP script! */
alert("PHP response is: "+ data);
/* message is the input you send to the PHP script! */
// alert(message); -> this is incorrect
}
请在此更改后通过运行父脚本 ( superadminviewbtn.php
) 重试。它奏效了吗?
推荐阅读
- php - MediaWiki 样式表未加载 - 仅在 MainPage 上正常运行 // DependencyError: Unknown module: mediawiki.page.startup
- database - 打开ubuntu防火墙和mongod.conf后无法远程连接mongodb
- android - 对话框关闭时如何切换现有片段?
- python - 如何在 CSV 文件中查找单词的频率?
- json - vue axios中如何通过id删除数据
- php - 如何正确访问从 PDO::FETCH_ASSOC 返回的数组?
- python - 同时将订阅的消息从 MQTT 写入 InfluxDB
- c++ - 返回布尔值不打印任何内容,也不显示任何编译器错误
- apache-spark - Kryo 序列化失败:缓冲区溢出。可用:0,必填:110581
- php - 如何在没有用户干预的情况下在某些条件下从 php 发送电子邮件?