JQuery函数没有发布到php

时间:2013-06-23 19:41:03

标签: php jquery ajax

我不是一个很大的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

}
?>

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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'); 
    }
});