Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
192
rated 0 times [  193] [ 1]  / answers: 1 / hits: 38554  / 11 Years ago, fri, november 22, 2013, 12:00:00

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.



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


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

?>


I am, however getting this error message from php:




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




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


More From » php

 Answers
7

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

[#74128] Wednesday, November 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neilshamarh

Total Points: 181
Total Questions: 94
Total Answers: 104

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
;