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

I am trying to send ajax POST to a php file, however the php file sends a "undefined index" notification, and the php file never gets the value I'm trying to send. I was looking for the answer to why this is not working properly, so hopefully someone can give me some insight.

My javascript function gets the value from html and gets the correct value. (in this case it is "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 the part of my php file that should receive the message:

    <?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, however, get this error message from php:

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

And my javascript always displays a warning in the error: "Error: failed to delete"

, , , - , , , . ( )...

+4
4

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

- , , , .

, . dataType:'json'

, script , "" , . PHP:

echo json_encode($something);

0

:

$val = $_POST["vals"];

:

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

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

All Articles