Problems with AJAX

Asked

Viewed 44 times

2

I’m trying to delete a photo with ajax.

I was able to collect the data (location of the photo) and now I was trying to receive that same location with another . php file .

What is failing me is that same "transfer". I think I did everything well, but I still can not receive the data.

This is the code

<script type="text/javascript">
$( ".delete" ).click(function() {
    var element = $(this);
    var original_url = element.attr('original_place');
    var thumbnail_url = element.attr('thumbnail_place');

if (confirm('Tem a certeza?')){
    $.ajax({
        type: 'POST',
        url: 'apagar_foto.php',
        data:{foto_original:original_url,foto_thumb:thumbnail_dir,},
        success: function()
        {
            alert('Eliminado');
        }       
    });
};
return false;
});
</script>

As you can see, I store the photo locations in 2 variables.

After a small confirmation I send

Code that was supposed to receive the data

$foto_original = $_POST['foto_original'];
$foto_thumb = $_POST['foto_thumb'];

echo "<script type='text/javascript'>alert('$foto_original');</script>";
echo "<script type='text/javascript'>alert('$foto_thumb');</script>";

Can you tell me where the mistake might be? Sorry for this simple question, but it hasn’t left me for some time. Thank you.

  • 2

    You can better check what happens when you use ajax if you include a parameter in the function success. Note the values of this parameter in the browser tools (most open them through F12). You see the return text passed to echo?

  • @Renan The functionality of the texts I have is simply to inform me that the data were received.

  • Is Alert 'Deleted' running? Others will never run, are scripts that you never include on the page

  • @bfavaretto Alert is not executed, nor is the data sent. The scripts (Echos) are in another . php file, such as: delete_foto.php. And their goal is to "alert" when the data gets there.

  • 1

    In PHP file, do print_r($_POST) and the return of AJAX, in the function success, place: success: function (data) { console.log(data); }. Run the code and see if any returns appear on the page console.

  • Okay, I deleted the previous comment, since you had a typo of my own, sorry. The result is an array with two lines, which represent the content I want to send.

  • 1

    So your AJAX is sending and receiving the values as expected, just like PHP is receiving. Just develop the logic you want now. Remember that alert on the PHP page will never be displayed, as this code will not run in the browser.

  • you can put Eval(data) to run php echo as javascript inside ajax Success, or follow Anderson’s tip, which is the best. To delete the file use: unlink('path') http://php.net/manual/en/function.unlink.php

  • Thank you all, I already solved the problem. The data were sent after all, but I had no idea that, since, I did not see the Alerts. Thank you again :D

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.