【问题标题】:Undefined Index in PHP after Using AJAX使用 AJAX 后 PHP 中未定义的索引
【发布时间】:2017-04-11 04:47:35
【问题描述】:

似乎这个问题已经被问过很多次了,但没有一个解决方案对我有用。我是 AJAX 的新手,所以也许我错过了一些基础知识?基本上我只是想将一个 html 段落的内容传递给一个 PHP 脚本,我不想使用 html 表单。

我有两个文件:tes.html

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type="text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
       $("#post").click(function(){
          var getContent = $("#text").html();
          $.ajax({
             url:"tes.php",
             type:"POST",
             data:{text:getContent},
             dataType:"html",
          });
       });
     });
</script>
</head>
<body>
    <p id="text" contenteditable="true">This is the content</p>
    <a href="tes.php" target="_blank"><button id="post">post 2</button></a>
</body>
</html>

和 tes.php

<?php
    if (isset($_POST['text'])) {
        $content = $_POST['text'];
        echo $content;
    } else {
        echo "no content";
    }
?>

点击 Post 按钮后,在 PHP 文件中返回

注意:未定义索引:第 3 行 C:\xampp\htdocs\projects\lab\tes.php 中的文本。

哪里出错了?真的需要你们的帮助。谢谢!

【问题讨论】:

  • 为什么错误位于 tes.php 而你链接 test.php?
  • 对不起,在原始代码中它实际上链接到 tes.php。我更改了这个问题的代码。
  • 好吧,未定义的索引只能意味着服务器没有收到 $_POST['text'] 变量...如果您删除 $content = $_POST['text']; echo $content; 并用 print_r($_POST); 替换它会发生什么(仅用于调试)
  • 我注意到它现在在第 3 行显示...这意味着变量已设置...我很困惑..这个错误通知实际上与代码相关吗?
  • 它返回了一个空数组.. Array()

标签: php jquery ajax


【解决方案1】:

你可以试试这个

  $(document).ready(function(){
   $("#post").click(function(){
      var getContent = $("#text").html();
      $.ajax({
         url:"tes.php",
         type:"POST",
         data:{text:getContent},
         dataType:"html",
         success:function(data) {
          console.log(data)
         }
      });
   });
 });

在 HTML 中(如 reza 建议的那样)

<button id="post">post 2</button>

然后,打开开发者控制台,看看是否能看到 tes.php 的响应

【讨论】:

  • 嗨@Christian!是的,我可以看到响应。非常感谢!还要感谢@reza
【解决方案2】:

修改html

<a href="tes.php" target="_blank"><button id="post2">post 2</button></a>

<button id="post">post 2</button>

【讨论】:

    【解决方案3】:

    到目前为止,您有一个 click 事件并且发送了一个 AJAX 请求,非常好。你有

    target="_blank"
    

    确保页面在另一个选项卡/窗口中打开。据我了解,你只想发送请求,所以你需要防止默认:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Test</title>
    <script type="text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
           $("#post").click(function(e){
              e.preventDefault();
              var getContent = $("#text").html();
              $.ajax({
                 url:"tes.php",
                 type:"POST",
                 data:{text:getContent}
              });
           });
         });
    </script>
    </head>
    <body>
        <p id="text" contenteditable="true">This is the content</p>
        <a href="tes.php" target="_blank"><button id="post">post 2</button></a>
    </body>
    </html>
    

    新的标签/窗口会给你错误,因为预期的参数不存在,所以不要打开它。

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      相关资源
      最近更新 更多