Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:25:02+00:00 2026-06-17T13:25:02+00:00

I am using wordpress as my cms and I am devloping a voting system.

  • 0

I am using wordpress as my cms and I am devloping a voting system. I want this code to run only on click event but it runs on every page load

How can i only make it run on click even

<script>
function addvote(){
 alert("<?PHP hello(); ?>");
}
 < /script>

 <?PHP
FUNCTION hello(){
   $postid="79";
   $current_votes = get_post_meta($postid, "votes", true);
$new_votes = intval($current_votes) - 1;
update_post_meta($postid, "votes", $new_votes);
$return = $new_votes>1 ? $new_votes." votes" : $new_votes." vote";
echo $return;
 }

?>

<button onclick="addvote()">Vote Now</button>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:25:03+00:00Added an answer on June 17, 2026 at 1:25 pm

    You cannot invoke PHP from JavaScript like that. You need AJAX: http://en.wikipedia.org/wiki/Ajax

    Also, use unobtrusive JavaScript – attach event handlers in your Script code, not in the HTML. Basic example how to implement what you probably want, using jQuery:

    // In your HTML page:
    <scipt type="text/javascript">
        $(document).ready(function() {
            $('button').click(function() {
                $.get("/hello.php", function(data) {
                    alert(data);
                });
            });
        });
    </script>
    
    // In hello.php:
    <?php
    function hello() {
        // ...
        echo $return;
    }
    
    hello();
    ?>
    

    You need to understand the distinction between server- and client-side processing. All your PHP code is executed on the server on each page request. The results are then sent to your browser, which displays them. Now there can be JavaScript code in your page, which is executed by the browser after the page is loaded. In order to invoke PHP code on a button click (or similar), this client-side code needs to make a new HTTP request to the server – this is what AJAX is for. This new request will trigger the execution of another PHP script. The results are transferred back to the JavaScript function after it finishes, and can then be used to do something, like adding additional content to your page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using WordPress as a CMS, and I want to extend one of its
1) I'm using WordPress as a CMS for a site. Therefore I want to
im using wordpress as a CMS for my personal portfolio website. i want to
Im using Wordpress as a CMS for this website. Basically the site has products
Possible Duplicate: jQuery keypress() event not firing? I am using wordpress as my cms
I am using wordpress as my cms and building a website. I use Graphene
I just started out using wordpress as a CMS. On the site I have
I'm using a cms (Wordpress) and have a gaggle of external stylesheets I'm loading
I am using wordpress as a full on CMS on a site I am
I have a few questions regarding website creation using the Wordpress CMS. With small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.