0

I want to call a PHP function when someone clicks an html link. I don't know how to do it. Please help.

<a href="" onclick="sendcode()">Didn't get a code?</a>
<?php
function sendCode(){
//code
}
?>
Zeeshan Ahmed
  • 85
  • 1
  • 2
  • 9
  • 3
    PHP is server side, HTML/JS is client side. In order for this to work, you would need a JS / AJAX function to execute on the click - making an additional request to the server via POST / GET request, you can not call a PHP method, as there is no longer a context of PHP on the client side device. – Matt Clark Feb 19 '17 at 05:55
  • I am new to web development. Can you tell me how can I use AJAX? – Zeeshan Ahmed Feb 19 '17 at 05:56
  • 2
    Probably, Google also can, I gave you a place to start looking. – Matt Clark Feb 19 '17 at 05:57

1 Answers1

0

Do it like this:

<a href="?sendcode=true">Didn't get a code?</a>
<?php
if(isset($_GET['sendcode'])){
    sendCode();
}
function sendCode(){
    //code
}
?>
Mohammad Rezaei
  • 241
  • 3
  • 16
  • Thanks mate this worked. Is there any flaw to using this? Any that I should know in terms of security? – Zeeshan Ahmed Feb 19 '17 at 06:04
  • This is most common way uses for run codes server side by a GET method request, and there is not any security issue while you are using this. most security issues come when you try to interact with databases or execute shell commands – Mohammad Rezaei Feb 19 '17 at 06:21