ajax passing parameter to php file not working
425
UPDATE: the php and javascript was updated/changed, based on the intial user comments:
This javascript does not get the response... what to change? How to make the javascript get the success or failed response from the php?
jQuery('.btndel').on('click', function(e){
e.preventDefault();
alert("click");
jQuery.ajax({
url: '/file.php',
type:'POST',
dataType: 'json',
data: { p: 'test' },
success: function( data ) {
response = JSON.parse(data);
if (response['success']){
alert("success");
} else {
alert("fail");
}
},
});
});
file.php
<?php
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(404);
error_log( '404 Not Found');
exit;
}
header('Content-type: application/json');
error_log("=====".$_POST['p']);
if (!isset($_POST['p'])) {
http_response_code(400);
error_log( "NO PARAM");
exit;
}
$response = array();
if ($update_success === false) {
$response['status'] = 'error';
} else {
$response['status'] = 'success';
}
exit(json_encode($response));
?>
what is wrong or missing?
Answer
Solution:
try to pass parameters like this!
https://qiita.com/art_porokyu/items/ffb08251a83e4fe46d57