Search code examples
phpfunctionhref

Call a php function (not on a file) from a href


Is there a way to make this <a> element do a php function or action when clicked, so when the item is clicked, instead of navigating to a php page, simply call the function and execute it, so when you click on it it does this(for example) echo 'FOO';


Solution

  • Clicking is only done on the client-side. So javascript is the best tool to execute the function.

    You could do something like:

    <a onclick="alert('foo');" ...
    

    If you must have server content as a result of the click, then you'll want to have javascript issue an AJAX call to get the content and then display it. Check into the jQuery library. It handles those types of things very, very well.