weixin_33711641 2013-08-22 13:16 采纳率: 0%
浏览 35

onclick删除文件和MySQL

So, I really love coding in php now, and I'm currently working against a database.

I think I might need to learn some AJAX too, but I'll ask you guys before I jump into another language.

Ok, so I'm currently coding an "edit.php" page to edit details about a project. The project can have several image files attached to it, and it's all linked up and working.

On each of these files, I have a delete button, so that the user is able to delete the files that are already uploaded (and remove the entry from the database).

For now, my code only reads and displays the content in the database, but no feedback is given to actually delete the image if the user clicks the delete icon.

Here is the image code:

<p>
    <a href="#" onclick="this.parentNode.parentNode.removeChild(this.parentNode)"><img src="assets/ux/delete_12px.png" width="12px" height="12px" style="margin:0px 5px -2px 0px" /></a>
    <span style="font-weight:bold">[<span style="color:#36F">Picture</span>]</span>&nbsp; 
    <span style="color:#666"><?=$rowImgFile['fileURL']?></span>
</p>

So, when you hit delete icon now, the image will be gone visually. I also need to delete it on the server.

I do know how to do this with php and mysqli, but how do I do that inside a function? What would be the best way to do this?

I've seen in another page that the onclick function of the delete icon is triggering a page refresh, adding ?deletepicture="picture" to the URL. Is this the best way to do it? (i dont want the page to refresh when deleting an image. I just want to execute my php code and mysqli code and get it away =)

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • csdn产品小助手 2013-08-22 13:34
    关注

    I think I might need to learn some AJAX too, but I'll ask you guys before I jump into another language.

    AJAX is a protocol, not a language (yep). The language is called "JavaScript" and you're already using it: it is just a matter of a few lines of code.


    (i dont want the page to refresh when deleting an image. I just want to execute my php code and mysqli code and get it away =)

    If you want to prevent the page from reloading you can either open the delete request in a new window (which is horrible) or learn to code an AJAX request. In your case, it would be something similar.

    Replace

    onclick="this.parentNode.parentNode.removeChild(this.parentNode)"
    

    with

    onclick="deleteImage(this)"
    

    and put this JS anywhere in your page (I assume you have jQuery included).

    <script>
    function deleteImage(domEl){
    domEl.parentNode.parentNode.removeChild(domEl.parentNode);
    var imageNumber = "-- retrieve here the image identifier you'll need in the PHP page";
    $.post("/yourServer/deleteImage.php", imageNumber);
    }
    </script>
    

    Using this script you may call actions on a PHP file without reloading the entire HTML page. Note that, in order to delete an image from a mysqli database, you'd probably need a unique identifier of this image. Since you didn't asked, I assume you already know how to get it in javascript. I also assume you already know how to code the file deleteImage.php in order to delete the data from the db after retrieving the image identifier from $_POST or $_GET.

    How would I send 2 variables with that POST functions? projectID and imageURL

    $.post("url", {projectID : "123", imageURL: "234"});
    

    This {} is standard js object literal notation. So also this syntax is valid.

    var args = {};
    args.projectID = "123";
    args.imageURL = "234";
    $.post("url", args );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable