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:

<div id="yesButton" class="button">Yes</div>
<script type="text/javascript">
  document.getElementById("yesButton").onclick = function () {
    $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(); ?>";

What am I doing wrong?

  • 1
    your php is executed when the page is rendered, you can't run php inside javascript like that. start by reading about the AJAX API. – Milo Apr 7 '14 at 21:25
  • @Milo Sure about that? I never registered an .onclick event/callback like that, but afaik the callback isn't executed during render time. The problem seems to be more the problem that it's not using AJAX, so the PHP gets executed and then the problem that there's no switch depending on any input the user made. But overall you are right. Still leaving this one open as it's an interesting topic. – kaiser Apr 7 '14 at 23:34
  • @kaiser - The js callback isn't executed, I meant the php is. – Milo Apr 7 '14 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.