Update a post’s info when clicking on a javascript button

I have a series of posts about various locations. I need people to verify if the location informaton is correct. The question is asked with two buttons for options. If “no” is clicked, they are taken to the edit page. If “yes” is clicked, the person’s name is updated as the last person to verify the listing and the page is refreshed. My problem is that the person’s name is updated whether or not they click on “yes”. The wp_update_post is being run whther they click on the butin or not.

Here is my code:

Read More
<div id="yesButton" class="button">Yes</div>
<script type="text/javascript">
  document.getElementById("yesButton").onclick = function () {
    <?php
    $post_id = get_the_ID();
    $user_id = get_current_user_id();
    $my_post = array(
      'ID'           => $post_id,
      'post_author' => $user_id
    );
    wp_update_post( $my_post );
    ?>
    location.href = "<?php echo get_permalink(); ?>";
  };
</script>

What am I doing wrong?

Related posts

Leave a Reply