Undefined index: Error in ajax POST and/or php script?

前端 未结 4 1192
清歌不尽
清歌不尽 2021-01-13 22:12

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

相关标签:
4条回答
  • 2021-01-13 22:30

    Change Ajax syntax...

    $.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");
        }
    );
    
    0 讨论(0)
  • 2021-01-13 22:31

    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");
          }
    });
    }
    
    0 讨论(0)
  • 2021-01-13 22:46

    Instead of:

    $val = $_POST["vals"];
    

    use this:

    if (isset($_POST['vals']) {
        $val = $_POST['vals'];
    }
    
    0 讨论(0)
  • 2021-01-13 22:50

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

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

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

    echo json_encode($something);

    0 讨论(0)
提交回复
热议问题