简体   繁体   中英

html onclick won't trigger php function

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.

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!";  
      }
  ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM