-1

Php does not receive a variable through $ _POST. I'm trying to pass a variable with ajax to a php page, but php takes a variable as NULL. Tell me, what is the error and how to fix it?

jquery code:

var imgPath;


$(".close_modal_clear_cover").on("click", function(e) {

    imgPath = $("#cover_preview").attr('src');
    $.ajax({
    url: "/management/ajax_scripts/img_delete.php",
    type: "POST",
    data: imgPath,
    async: true,
    cache: false,
    contentType: false,
    dataType: "json",
    processData: false,
    success: function (returndata) {
            console.log(imgPath); //url of image
            console.log(returndata); // NULL
        }
    });

});

img_delete.php code:

if (isset($_POST['imgPath'])) {
$path= $_POST['imgPath'];
unlink($path);
$response = $path; 

} else {
  $response = "false";
}

echo json_encode($response);
Osigot
  • 27
  • 6
  • Seriously? You're clearly not looking in `$_POST` and are using `$_GET`. And you can't even claim you didn't know because yo use `$_POST` in your title. – John Conde Jun 08 '18 at 22:18
  • I experimented, trying to find the reason. The error with this is not related, actually there $ _post. – Osigot Jun 08 '18 at 22:24
  • for more reference, here is a question and detail answers on this...https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php – LearnToday Jun 09 '18 at 00:13

2 Answers2

0

data: imgPath should be data: {imgPath: imgPath} and you should change your $_GETs to $_POSTs in the php backend

Reflective
  • 3,854
  • 1
  • 13
  • 25
  • "data: imgPath should be data: {imgPath: imgPath}" - nothing changed and "change your $_GETs to $_POSTs in the php backend" - actually there $ _post, I made a mistake when I wrote the question. – Osigot Jun 08 '18 at 22:34
  • Open the developer tools in the browser you use ... usually F12 will open it ... go to the network tab and you will see all your network requests. There are generally 2 main sections for Request body and headers and for response body and headers. Se what you are really sending. – Reflective Jun 08 '18 at 22:41
  • `!isset($_POST['imgPath'])` should be `isset($_POST['imgPath'])` i.e. `if set` ... you should be very strict what you are writing ... too much errors until now – Reflective Jun 08 '18 at 22:45
  • maybe I'm not very attentive, I agree. But from this the essence of the problem has not changed. As before, php does not receive a variable. – Osigot Jun 08 '18 at 23:09
  • Did you see what happens in Network tab in Developer Tools - this tab should be your best friend if you want to write a callback to any backend! – Reflective Jun 08 '18 at 23:11
0

Thanks @Reflective for the help. Having studied what data comes in img_delete.php and a little googling, I found a solution. At me for some reason contentType was specified as false, but it was necessary 'application / x-www-form-urlencoded; charset = UTF-8 '.

This is a stupid mistake, but suddenly someone with the same collided.

$(".close_modal_clear_cover").on("click", function(e) {
    // imgPath = $.session.get("imgCover");
    imgPath = JSON.stringify($("#cover_prewiew").attr('src'));
    $.ajax({
    url: "/management/ajax_scripts/img_delete.php",
    type: "POST",
    data: {imgPath: imgPath},
    async: true,
    cache: true,
    contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    dataType: "json",
    processData: true,
    success: function (returndata) {
            console.log(imgPath);
            console.log(returndata);
        }
    });

});
Osigot
  • 27
  • 6