I am searching for a simple solution to call a PHP function only when a-tag is clicked.
PHP:
function removeday() { ... }
HTML:
<a href="" onclick="removeday()" class="deletebtn">Delete</a>
UPDATE: the html and PHP code are in the same PHP file
It can be done and with rather simple php if this is your button
and this is your php code
the code get's called when submit get's posted which happens when the button is clicked.
Try to do something like this:
In javascript, make an ajax function,
Then call from html,
And in your ajax.php,
First, understand that you have three languages working together.
PHP: Is only run by the server and responds to requests like clicking on a link (GET) or submitting a form (POST).
HTML & JavaScript: Is only run in someone's browser (excluding NodeJS).
I'm assuming your file looks something like:
Because PHP only responds to requests (GET, POST, PUT, PATCH, and DELETE via $_REQUEST), this is how you have to run a PHP function even though they're in the same file. This gives you a level of security, "Should I run this script for this user or not?".
If you don't want to refresh the page, you can make a request to PHP without refreshing via a method called Asynchronous JavaScript and XML (AJAX).
That is something you can look up on YouTube though. Just search "jquery ajax"
I recommend Laravel to anyone new to start off right: http://laravel.com/
This is the easiest possible way. If form is posted via post, do php function. Note that if you want to perform function asynchronously (without the need to reload the page), then you'll need AJAX.
Try this it will work fine.