2013-06-23 34 views
0

我不是一个大家伙Jquery的,但现在用一个插件,它是一个图片上传。它有一个按钮,用于删除与以下代码相关的图像。jQuery函数不张贴到PHP

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  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'file reached'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\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 

} 
?> 

开头的电子邮件的东西在里面只是想获得它做什么。该文件的路径是正确的。

+0

在同一个目录中的文件?控制台上的任何错误? – Sergio

+0

你确定它是一个发布请求吗? – KyleK

+0

确保参数'url'是正确的,即:将链接到文件'remove_unwanted_files.php'是正确的 – bystwn22

回答

1

首先,我会确保AJAX--to--PHP系统工作。你可以这样做测试有两个小的变化:

1)在你的PHP文件的最顶部,只是让回声出一个简单的字符串和死亡。例如:

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

2)在你的AJAX成功的功能,把警报()来捕获/显示来自PHP文件的输出。例如:

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

做这两个简单的更改会立即显示错误的位置。如果错误出现在您的PHP文件中,则发布另一个问题并询问有关情况(当然,发布PHP代码)。

下一个测试,我建议,是(非常轻微修改初试)做出来的通过AJAX接收数据的PHP文件回显:

<?php 
    $f = isset($_POST['file_to_remove']) ? $_POST['file_to_remove'] : ''; 
    die('PHP recd from AJAX: ' . $f); 
+0

错误最终出现在我的PHP代码中。这个答案让我能够快速诊断并修复它。感谢大家的帮助。 – KyleVan

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'); 
    } 
}); 
+0

查询字符串或对象,都将在'POST'中工作 – bystwn22

+0

不是我不相信它的真实......但我从来没有见过它的工作没有毛刺......每当我将数据作为对象传递给我时总是遇到更少的问题.....我不知道为什么.....只是给我经常遇到的事情提供帮助,并且每当我尝试查询字符串后....我有问题。 – KyleK

+0

如果你传递一个对象,jQuery会为你编码数据,如果你传递一个字符串,你必须确保它自己编码正确。 – Musa