Categories: javascript, php, html, css, server

Exectue PHP Function from <button onclick=“”>

1 answer

I want to execute a PHP Script, when i Click an HTML Button. I know, that i cant do this: <button onclick="myPhpFunction("testString")">Button</button> or something similar. My goal is, that i execute some Commands on my Server when clicking this Button. Note: My php Function need variables from the JS/HTML.

Is there a way to do this?

Received answer to this question:
The best answer according to the author of the question:

already answered here :- Run php function on button click

one more example from my side:-

How to call PHP function on the click of a Button ?

<h1 style="color:green;">      GeeksforGeeks  </h1>   <h4>      How to call PHP function      on the click of a Button ?  </h4>   <?php     if(array_key_exists('button1', $_POST)) {          button1();      }      else if(array_key_exists('button2', $_POST)) {          button2();      }      function button1() {          echo "This is Button1 that is selected";      }      function button2() {          echo "This is Button2 that is selected";      }  ?>   <form method="post">      <input type="submit" name="button1"             class="button" value="Button1" />       <input type="submit" name="button2"             class="button" value="Button2" />  </form>