weixin_33739523 2015-10-22 17:51 采纳率: 0%
浏览 19

使用AJAX发送PHP值

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!

  • 写回答

1条回答 默认 最新

  • elliott.david 2015-10-22 18:22
    关注

    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

    评论

报告相同问题?