Skip to content
Advertisement

Calling a PHP function on button click?

I have a html button which has an onclick method. The function which is ran onclick has a php function in but is called in JS, like below.

<script type="text/javascript">
    function Test() {
      <?php add_post_meta($post->ID, '_my_meta_key', true); ?> 
    }
</script>
<button onclick="Test()"></button>

The page this code is on is a wordpress admin page. When the page loads, the php function is ran anyway instead of waiting for the button click event. Is there a way to stop this from happening and only fire once the button is clicked.

Advertisement

Answer

You cannot directly call a php function using a button click.But you can call Js functions on button click event. And then you can implement Ajax to call php functions.

You are using wordpress so you can use wp functions or custom coding to achieve this.

Php is server-side scripting language. Which will run only on server.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement