duanci6484 2013-06-23 19:41
浏览 42
已采纳

JQuery函数没有发布到php

I am not a big Jquery guy but am using a plugin which is an image uploader. It has a button to remove images with correlates with the following code.

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;
}

However, none of the code within the PHP file is ever executed. The alert does pop up and asks you if you want to delete the file with the correct file name. The PHP code is what actually deletes the file however the file does not delete from the file system. I have tried putting other code in the PHP file like sending me an email and none of it executes. Can anyone see anything wrong with this JQuery code?

THIS IS THE PHP CODE

<?php

$to      = 'dev@kylevan.com';
$subject = 'the subject';
$message = 'file reached';
$headers = 'From: noreply@mobilehomelist.com' . "
" .
    'Reply-To: noreply@mobilehomelist.com' . "
" .
    '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

}
?>

The email stuff at the beginning is in there just trying to get it to do something. THe path to the file is correct.

  • 写回答

2条回答 默认 最新

  • dpqmu84646 2013-06-23 20:22
    关注

    First I would make sure the AJAX--to--PHP system is working. You can do that test with two small changes:

    1) At the very top of your PHP file, just make it echo out a simple string and die. For example:

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

    2) In your AJAX success function, put an alert() to capture/display the output from the PHP file. For example:

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

    Doing these two simple changes will immediately show you where the error is. If the error is in your PHP file, then post another question and ask about that (posting the PHP code, of course).

    The next test, I would suggest, is (a very slight modification to the first test) to make the PHP file echo back out the data received via AJAX:

    <?php
        $f = isset($_POST['file_to_remove']) ? $_POST['file_to_remove'] : '';
        die('PHP recd from AJAX: ' . $f);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像