SaucedApples SaucedApples - 2 years ago 84
PHP Question

OnClick activating on page load/refresh

I have a link which, when the page is called or refreshed, runs.

As this is set to execute onclick, this should not be happening.

I had a sneaking suspicion that because I had left the path empty in

that it might be causing the issue, but adding in a file location does not change the outcome.

<a href="" onClick="<?php exec('sudo python /var/www/html/'); ?>"><img src="lightm.png" width="128" height="128"></a>

Answer Source

I have no experience with the exec function, but somehow my gut feeling tells me it will execute no matter where it is in a HTML document. Actually, it's like any function with PHP, you don't actually need it to be formatted properly in an HTML file, it will run anyway (well, unless it's commented out), so that's the reason it calls every time, I guess...

Best if you use AJAX instead or some other means to load your external file, not a PHP call (server), considering you're using onclick that returns the click event handler code (client).


<a href="" onClick="myFunc()"><img src="lightm.png" width="128" height="128"></a>

The AJAX call:

function myFunc() {
    url: "/var/www/html/",
    // rest of the AJAX magic
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download