首页 > 解决方案 > 如何将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

错误截图

标签: phpajax

解决方案


你必须纠正你的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) 重试。它奏效了吗?


推荐阅读