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 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误