0

I have a button that's supposed to call a php function when it's called.

I've tried onClick, onclick, onclick="",and some other variations.

<input  type="submit" name="test" id="test" value="Save" onclick="<? 
       php echo click(); ?>" /><br/>

I just want it to get the onclick and trigger the php function.

  • 1
    Possible duplicate of [Execute PHP function with onclick](https://stackoverflow.com/questions/19323010/execute-php-function-with-onclick) – catcon May 22 '19 at 02:11
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – miken32 May 22 '19 at 02:26
  • That code will execute the php function "click()" when then page is executed, and output the results of that function into the html attribute onclick. Have a look at AJAX programming if you want to call php functions after page load. – Grant C. May 22 '19 at 03:31

1 Answers1

1

The onclick attribute in HTML calls Javascript functions, not PHP functions.So you can use JS. Otherwise you can use the html for tag and use GET/POST method to call php function on click of the button in order to get it done.

The html code to be used.

<form action="" method="post">
  <input type="submit" name="test" value="Save" />
</form>

This is your php code

<?php
       if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['test']))
      {
          click();
      }
      function click()
      {
          echo "Clicked me!";  
      }
  ?>
Makdia Hussain
  • 744
  • 3
  • 11