I'm working on a task for a client that I thought would be simple. A remote computer uploads a file upon some event. My site needs to display a flag/statement if that file exists. So, upon page load it checks if the file exists. That all works fine and dandy.

The problem is that the user wants to have a reset button. In other words, after he sees that the flag is set, he wants to be able to click a button or text link and delete that file. I want to use something like this.

<?php
if (file_exists($triggerfile)) {
echo '<input type="button" value="Delete" onClick="'.unlink($triggerfile).'">';
} else {
    echo "The file $triggerfile does not need to be reset";
}
?>

Initially I thought that I had this, but now I realize that it's not really working as I had thought. Instead of deleting the file with onclick, it's falling through that code and deleting that file whether someone clicks or not.

Can anyone tell me what I'm doing wrong here or how I can accomplish this simple task?

Thanks in advance for your help!

Member Avatar for diafol

You can use ajax to delete a file without forcing a page reload. Your code won't work as js cannot run php which has already run.

PHP runs before it arrives at the client. It's then that js gets hold of the then pure html output. As far as js is concerned, there's only html, no php, so it can't run it.

Ajax is a js call to an external php page - the php file does its thing and possibly return a value / data to the js script. The js script then does something with this, e.g. update an area of the page.

Hmm. I'm afraid I'm not familiar with Ajax at all. I probably should have also mentioned that this is all occurring on a wordpress page. I'm not sure that would make any difference.

Any recommendations about where to look for easy to understand tips etc.?

Member Avatar for diafol

How about posting to a wordpress forum?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.