0

我不是一个大 Jquery 人,但我正在使用一个插件,它是一个图像上传器。它有一个按钮来删除与以下代码相关的图像。

function remove_unwanted_file(id,file)
{
    if(confirm("If you are sure that you really want to remove the file "+file+" then     click on OK otherwise, Cancel it."))
{
    var dataString = "file_to_remove=" +file;
    $.ajax({
        type: "POST",
        url: "remove_unwanted_files.php",
        data: dataString,
        cache: false,
        beforeSend: function() 
        {
            $("#vpb_remove_button"+id).html('<img src="images/loadings.gif" align="absmiddle" />');
        },
        success: function(response) 
        {
            $('div#fileID'+id).fadeOut('slow'); 
        }
    });
}
return false;
}

但是,PHP 文件中的任何代码都不会被执行。确实会弹出警报并询问您是否要删除具有正确文件名的文件。PHP 代码是实际删除文件的内容,但文件不会从文件系统中删除。我尝试将其他代码放入 PHP 文件中,例如向我发送电子邮件,但没有一个执行。任何人都可以看到这个 JQuery 代码有什么问题吗?

这是 PHP 代码

<?php

$to      = 'dev@kylevan.com';
$subject = 'the subject';
$message = 'file reached';
$headers = 'From: noreply@mobilehomelist.com' . "\r\n" .
    'Reply-To: noreply@mobilehomelist.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

include('ASApiClientServer.php');
include('ASApiClient.php');
$uniquekey=$_SESSION['uniquekey'];
$postFields = array(
    'referralCode'=>$uniquekey,
    'image'=>strip_tags($_POST["file_to_remove"]),
    ),
);
    $ApiClient = new ASApiClient();
try{
        $ApiClient->requestResponse('removePicture', $postFields);
        //handle the call
    }
    catch(Exception $e){
    print_r($ApiClient->getRawResponse());
}

if(isset($_POST["file_to_remove"]) && !empty($_POST["file_to_remove"]))
{
    $uploaded_files_location = 'uploaded_files/'.strip_tags($_POST["file_to_remove"]);

    @chmod($uploaded_files_location,0777);
    @unlink($uploaded_files_location);

    //Here you can also delete the file from your database if you wish assuming you also saved the file to your database during upload

}
?>

一开始的电子邮件内容只是试图让它做某事。文件的路径是正确的。

4

2 回答 2

1

首先,我会确保AJAX--to--PHP系统正常工作。您可以通过两个小改动来进行该测试:

1) 在你的 PHP 文件的最顶部,让它回显一个简单的字符串并死掉。例如:

<?php
    die("I got to here");

2) 在您的 AJAX 成功函数中,放置一个 alert() 以捕获/显示 PHP 文件的输出。例如:

success: function(response) 
{
    alert("AJAX Recd from PHP: " + response);
    //$('div#fileID'+id).fadeOut('slow'); 
}

做这两个简单的改变会立即告诉你错误在哪里。如果错误出现在您的 PHP 文件中,请发布另一个问题并询问相关问题(当然是发布 PHP 代码)。

我建议下一个测试是(对第一个测试进行非常轻微的修改)以使 PHP 文件回显通过 AJAX 接收到的数据:

<?php
    $f = isset($_POST['file_to_remove']) ? $_POST['file_to_remove'] : '';
    die('PHP recd from AJAX: ' . $f);
于 2013-06-23T20:22:45.227 回答
0

首先确定你url是对的...

remove_unwanted_files.php是正确的道路,而不是像example/directory/remove_unwanted_files.php

然后......如果它实际上是一个post请求......试试这个......

 $.ajax({
    type: "POST",
    url: "remove_unwanted_files.php",
    data: {file_to_remove:file},
    cache: false,
    beforeSend: function() 
    {
        $("#vpb_remove_button"+id).html('<img src="images/loadings.gif" align="absmiddle" />');
    },
    success: function(response) 
    {
        $('div#fileID'+id).fadeOut('slow'); 
    }
});

如果它不是一个Post...

尝试这个...

var dataString = "file_to_remove=" +file;
$.ajax({
    type: "GET",
    url: "remove_unwanted_files.php",
    data: dataString,
    cache: false,
    beforeSend: function() 
    {
        $("#vpb_remove_button"+id).html('<img src="images/loadings.gif" align="absmiddle" />');
    },
    success: function(response) 
    {
        $('div#fileID'+id).fadeOut('slow'); 
    }
});
于 2013-06-23T19:43:38.413 回答