Nathan Cockerill
Nathan Cockerill

Reputation: 23

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.

Upvotes: 2

Views: 8762

Answers (2)

safin chacko
safin chacko

Reputation: 1390

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.

Upvotes: 4

tbobm
tbobm

Reputation: 123

You'll need to use AJAX. You can learn some useful things on w3school:
http://www.w3schools.com/ajax/

Upvotes: 1

Related Questions