【问题标题】:Pass variable to another page using AJAX [closed]使用 AJAX 将变量传递到另一个页面 [关闭]
【发布时间】:2017-08-11 05:27:22
【问题描述】:

## 这是我的 test.php 文件 ##

 //here I am getting the variable value

<?PHP
 $name = $_POST('variable');
 echo $name;
?>

## 这是我的脚本 ##

 //avariable value passing to test .php file

<script type ="text/javascript">
  var name = "jani";
  $.ajax({
          type: 'POST',
          url: 'test/test.php',
          data: {'variable': name},
          });
</script>

###** 我收到此错误 **###

 Fatal error: Function name must be a string in 
                 D:\xampp\htdocs\test\test.php on line 2

【问题讨论】:

标签: javascript php


【解决方案1】:

如下更改您的 test.php 代码 use square bracket not round for super global variable

<?php
if (isset($_POST['variable'])) {
    $name = $_POST['variable'];
    echo $name;
}
?>

【讨论】:

  • $name = $_POST['variable'];回声$名称;但我收到未定义的变量错误
  • @Jani 我已经更新了我的答案。检查一下
【解决方案2】:

试试这个肯定会奏效: 我的 PHP 文件:

<script>
var name = "name";
var url = "test/test.php";
        $.ajax({
            url: url,
            type: 'POST',
            data: {'name': name},
           success: function (data) {
            console.log(date);
           }
});
</script>

接收 ajax 请求的地方:

//here I am getting the variable value

<?PHP
 $name = $_POST['name'];
 echo $name;
?>

在您的最新评论之后,我添加了 js fiddle 链接 在运行它之前打开控制台和 POST 除了标题部分,您可以看到数据已经发布。 Js Fiddle Demo Link

【讨论】:

  • 未定义索引:第 5 行 D:\xampp\htdocs\test\test.php 中的名称出现此错误
  • 这次在发送ajax请求时检查控制台是不是在发送参数?
  • 未定义索引:D:\xampp\htdocs\test\test.php 中的名称
  • 您检查过控制台吗?你发送了什么参数?
  • 我已经更新了答案并添加了链接,你可以看到它
猜你喜欢
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
相关资源
最近更新 更多