0

I'm working on wordpress. I write this code to run php_function On onclick event. But everytime I reload the page, the php_function runs. In the other words, I want this function (php_function) just runs with Onclick event, Not page reload or anything else. How can i handle this issue? Any help would be apreciated. Thanks.

<input type="submit" class="button" value="Validate"
       onclick="document.write('<?php php_function( $post ); ?>');" 
/>
mvw
  • 5,075
  • 1
  • 28
  • 34
inverted_index
  • 2,329
  • 21
  • 40

1 Answers1

0

The php function runs because the whole lot is run on the server before the page is generated.

When a request is sent, the php script is executed fully and the HTML is built and sent back to the browser to be displayed.

You need to use JavaScript to respond to events like onclick

Toni Leigh
  • 4,830
  • 3
  • 22
  • 36
  • Thank you. So you mean there is not any way to trigger php function with Onclick event ?? – inverted_index Sep 24 '15 at 21:36
  • @sajastu read the linked question, it gives good detail - you can trigger a php function, but you need to use javascript (ajax) to send another request to the server because php is on the server, not in the browser. You should look at binding javascript events rather than using onclick attributes, as using onclick attributes will hurt in the future – Toni Leigh Sep 24 '15 at 21:37