doumen6605 2019-06-06 12:50
浏览 63

ajax问题:未定义索引,我应该怎么做?

i'm trying to learn ajax for a new project, i need to upload data using some javascripts variables, so in order to start , i decided to try a small easy ajax problem , but i didnt work,

Notice: Undefined index: name in C:\Apache24\htdocs\d.php on line 4 is shown

My .js file :

<script>

$.ajax({
    type: "POST",
    url: 'd.php',
    data: {name : "aa"},
    success: function(output) {
        alert(output);
    }
    error: function(request, status, error){
            alert("Error: Could not delete");
    }
);
        </script>

My php file:

<?php

print_r($_POST['name']);

?>

Thank you

  • 写回答

4条回答 默认 最新

  • dongxing7083 2019-06-06 13:03
    关注

    It doesn't work like that, you're never gonna see what went on behind the hood, the only way out is to log the request in txt file.

    Your variable would only be defined when ajax send the POST request, to see if it is successful, edit your code like this:

    <?php
      //if post is set
      if(isset($_POST['name'])) {
         //open log.txt
          $file = fopen('log.txt', 'a'); 
         //ensure print_r give a return
           fwrite($file, print_r($_POST, true);
         //close log.txt
           fclose($file); 
       }
    ?>
    

    Ensure you create a file log.txt

    After doing this send the ajax request and view the $_POST variables in log.txt

    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗