1

I am trying to delete images with Ajax and all the php seems to work except when I try to send variables to another php document.

Php that shows and grabs neccessary values.

// show images
    $image_display = "";
    foreach(glob($pathimages.'*') as $filename){
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        $name_only = basename($filename, ".".$ext);

        $image_display .= "<img src=\"images/" .$targetID."/" .$name_only.".".$ext. "\" width=\"30\" />
                        <a onclick=\"DeleteImage('".$name_only."','".$ext."','".$targetID"'); return false;\" href=\"javascript:;\">X</a>
                        <br />";
        }

.JS document, I get the sent and the success messages when pressing the X

function DeleteImage(name_only, ext, targetID){
$.ajax({
    url: 'delete_imgs.php',
    type: "POST",
    data:{name_only:name_only,ext:ext,targetID:targetID},
    beforeSend: function() {
    alert("sent");
    },
    success: function(html) {
        alert("Success") 
    },
    error: function( x, status, error ) {
        alert(x.status + status + error);
    }
});
}

delete_imgs.php document

include('session_check.php');
$name_only = $_POST['name_only'];
$ext = $_POST['ext'];
$targetID = $_POST['targetID'];

$pathimages = "images/$targetID/";

unlink($pathimages . $name_only .".". $ext);
echo "Deleted";

Any thoughts are more than welcome since I have banged my brain out of my head by now ...!

Cheers!

0

1 Answer 1

1

Try with async:false

function DeleteImage(name_only, ext, targetID){
$.ajax({
    url: 'delete_imgs.php',
    type: "POST",
    async : false,
    data:{name_only:name_only,ext:ext,targetID:targetID},
    beforeSend: function() {
    alert("sent");
    },
    success: function(html) {
        alert("Success") 
    },
    error: function( x, status, error ) {
        alert(x.status + status + error);
    }
});
}

Maybe that can help

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry!! This acctully seem to have helped a little! When I press the X and then refresh the page the image disappears. Now all I want to do is add so that I don't have to refresh everytime ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.