未定义索引:ajax POST和/或php脚本错误? [英] Undefined index: Error in ajax POST and/or php script?

查看:96
本文介绍了未定义索引:ajax POST和/或php脚本错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将ajax POST发送到php文件,但是php文件发送了未定义索引"的通知,并且php文件似乎从未收到我试图发送的值.我一直在寻找为什么它不能正常工作的答案,所以希望有人可以给我一些见识.

I'm trying to send an ajax POST to a php file, however the php file sends a notice of "undefined index", and the php file never seems to receive the value i'm trying to send it. I've been searching for the answer to why this isn't working correctly, so hopefully someone can give me some insight.

我的javascript函数从html接收一个值,并接收正确的值. (在这种情况下为"1")

My javascript function receives a value from the html, and receives the correct value. (it's "1" in this case)

    function deleteMediaFromDatabase(val)
    {

  $.ajax({ url: 'deleteMediaFromDatabase.php',
         data: {vals : val},
         type: 'post',
         success: function(output) {
                      alert(output);
                  },
          error: function(request, status, error){
            alert("Error: Could not delete");
          }
  });
}

这是我的php文件的一部分,应该会收到该帖子:

Here is part of my php file that should receive the post:

    <?php

 ini_set("display_errors", "On");
 error_reporting(E_ALL);

    $val = $_POST["vals"];

    // create connection
    $con = mysqli_connect(<stuff you don't care about>);

  error_log($val . ' is the value', 3, "./error.log");

?>

我在,但是从php收到此错误消息:

I am, however getting this error message from php:

注意:未定义索引:vals in /xxx/xxx/htdocs/AdminPanel/deleteMediaFromDatabase.php,第9行

Notice: Undefined index: vals in /xxx/xxx/htdocs/AdminPanel/deleteMediaFromDatabase.php on line 9

而且我的JavaScript总是在以下错误中输出警报:错误:无法删除"

And my javascript always outputs the alert in the error: "Error: Could not delete"

我知道这个问题已经被问过很多次,但是,除非我跳过一些小东西,否则我的代码对我来说看起来是正确的. (但并非总是如此...)

I know this question has been asked and answered many times, however unless I'm skipping over something small, my code, to me, looks correct. (but doesn't it always...)

推荐答案

jquery的语法错误.您错过了数据的语法.这应该是这样的-

There is error in syntax of jquery.. You missed out syntax of data. This should be like this-

function deleteMediaFromDatabase(val)
{
$.ajax({ url: 'deleteMediaFromDatabase.php',
     data: {'vals' : val},
     type: 'post',
     dataType:'json',
     success: function(output) {
                  alert(output);
              },
      error: function(request, status, error){
        alert("Error: Could not delete");
      }
});
}

这篇关于未定义索引:ajax POST和/或php脚本错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆