dtcu5885 2014-03-17 13:25
浏览 55
已采纳

在AJAX调用期间删除文件的扩展名

I have this script , trying to delete an image with AJAX call.

PHP

 foreach ( $in_folder as $img => $v ) {

    echo '
        <span class="imageHolder">
         <a onclick="DeleteImage('.$img.'); return false;" href="javascript:;">X</a>
         <img src="uploads/' . $img .'"/>
            <span class="uploaded">
            </span>
        </span>';
    // print_r($in_folder);
    }

The JS :

 function DeleteImage(img){

        $.ajax({
            url: 'delete.php',
            type: "POST",
            data: img,
            success: function(datos){
                console.log("check");
            }
        });
    }

delete.php

 if($_GET["action"]=="deleteimage")
    {
        $imagefile = $POST['img'];
        $img_path = 'uploads/' . $img;
        unlink ( $img_path ); 
    } 

The problem is the JS part , I am getting an error :

Error: ReferenceError: IMAGE_NAME is not defined
Source File: http://localhost/dev/_upload_test/
Line: 1

However ,the PHP $img variable is outputting IMAGE_NAME.jpg

<a href="javascript:;" onclick="DeleteImage(IMAGE_NAME.jpg); return false;">X</a>

Shouldn´t that error be

Error: ReferenceError: IMAGE_NAME.jpg is not defined

( where is the extention ?? )

Obviously my extention .jpg is being cut off by the ajax process , But since my JS skills are <= 0 , I am having trouble understanding what is getting wrong in the process ..

  • 写回答

2条回答 默认 最新

  • doulianxi0587 2014-03-17 13:26
    关注

    You need to change

    data: img,
    

    to

    data: { img:img },
    

    or use

    data: "img="+img,
    

    Also you need to wrap string using ' or " .

    <a href="javascript:;" onclick="DeleteImage('IMAGE_NAME.jpg'); return false;">X</a>
    <!--                                      --^--          --^--                  -->
    

    Change your php code to like this,

    foreach ( $in_folder as $img => $v ) {
    echo '
            <span class="imageHolder">
             <a onclick="DeleteImage(\''.$img.'\'); return false;" href="javascript:;">X</a>
             <img src="uploads/' . $img .'"/>
                <span class="uploaded">
                </span>
            </span>';
        // print_r($in_folder);
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错