未定义的索引:ajax POST和/或php脚本出错?

时间:2022-10-17 09:10:48

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.

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

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

我的javascript函数从html接收一个值,并接收正确的值。 (在这种情况下它是“1”)

    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");
          }
  });
}

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

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

    <?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");

?>

I am, however getting this error message from php:

我是,但从php获取此错误消息:

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

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

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

我的javascript总是在错误中输出警告:“错误:无法删除”

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...)

我知道这个问题已被多次询问和回答,但是除非我跳过一些小的东西,否则我的代码看起来是正确的。 (但不总是......)

4 个解决方案

#1


1  

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

jquery的语法有错误..你错过了数据的语法。这应该像这样 -

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");
      }
});
}

#2


0  

The problem can come from the dataType not being specified or that the dataType specified does not match thus returned by the server.

问题可能来自未指定的dataType,或者指定的dataType与服务器返回的不匹配。

Explicitely set the dataType, e.g. dataType:'json'

显式设置dataType,例如数据类型: 'JSON'

and make sure that your script returns data that is "encoded" in the data type that you chose, e.g. in PHP:

并确保您的脚本返回在您选择的数据类型中“编码”的数据,例如在PHP中:

echo json_encode($something);

#3


0  

Instead of:

$val = $_POST["vals"];

use this:

if (isset($_POST['vals']) {
    $val = $_POST['vals'];
}

#4


-1  

Change Ajax syntax...

改变Ajax语法......

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

#1


1  

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

jquery的语法有错误..你错过了数据的语法。这应该像这样 -

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");
      }
});
}

#2


0  

The problem can come from the dataType not being specified or that the dataType specified does not match thus returned by the server.

问题可能来自未指定的dataType,或者指定的dataType与服务器返回的不匹配。

Explicitely set the dataType, e.g. dataType:'json'

显式设置dataType,例如数据类型: 'JSON'

and make sure that your script returns data that is "encoded" in the data type that you chose, e.g. in PHP:

并确保您的脚本返回在您选择的数据类型中“编码”的数据,例如在PHP中:

echo json_encode($something);

#3


0  

Instead of:

$val = $_POST["vals"];

use this:

if (isset($_POST['vals']) {
    $val = $_POST['vals'];
}

#4


-1  

Change Ajax syntax...

改变Ajax语法......

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