douzhanlie9209 2014-05-08 08:11
浏览 59
已采纳

按下按钮时删除文件[关闭]

I want to delete the file when I pressed the button. My code is below

<html>
  <input type="button" value="Delete" onClick="if(confirm('Are you sure want to delete     Msg<?php $file="0000"; echo $file; ?>?'))
  <?php unlink("msg".$file.".wav");
        unlink("msg".$file.".gsm");
        unlink("msg".$file.".WAV");
        unlink("msg".$file.".txt");
  ?>
    alert('The Msg<?php echo $file; ?> is Deleted') 
else alert('The Msg<?php echo $file; ?> not Deleted')">
</html>

But it not works

  • 写回答

2条回答 默认 最新

  • 普通网友 2014-05-08 08:41
    关注

    There's a few problems here.

    Firstly, you don't ever finish your onClick attribute, so this is likely to result in JavaScript errors. Check this in your console. If you are using onClick it is best to keep JavaScript brief in here — write a JavaScript function, and call it from this event.

    Any PHP you have will be run when the page is served. Thus, your deletions will be done immediately, and not inside the JavaScript if statement as you want. You can achieve this in one of two ways:

    • Set up a post method form to do a trip to the server, in which you do your file deletions
    • Use an AJAX call to do a similar thing in the background. Libraries like jQuery will make this easy for you

    Lastly, it is unclear how you would want the $file to be populated. If this depend on the button you press on, you'll need to read that in either of the above approaches, and then detect the name on the server side.

    As others have said, there is no substitute for sitting down for a good bit of study with a book or a video. It's frustrating when you just want to make something, but you'll find it a good time investment in the long term. Stick at it!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题