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.

3
  • 1
    Possible duplicate of Execute PHP function with onclick Commented May 22, 2019 at 2:11
  • 1
    Possible duplicate of What is the difference between client-side and server-side programming? Commented May 22, 2019 at 2: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. Commented May 22, 2019 at 3:31

1 Answer 1

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!";  
      }
  ?>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.