1

After a lot of searching, I seem to have found that get_this_ID(); nor other iterations of it will work in an AJAX request. I'm admittedly a PHP noob, can someone please explain how this guy here accomplished it? He says he "Simply echoed the id into a hidden div, then passed it back with the AJAX call."

My code is below.

if(wp_verify_nonce($nonce, 'checkbox') !== false) {

     global $post; 
     $post = $wp_query->get_queried_object();
     $post_ID = $post->ID; 

      $user_ID = get_current_user_id();

       $dataArray[$post_ID] = isset($dataArray['second_checkbox']) ? true : false;

      if($user_ID != NULL) {
        foreach($dataArray as $key=>$value) {

          $status = update_user_meta($user_ID, $key, $value);

        }

        //ajaxStatus('success', 'Meta fields updated.', $post_ID);
        ajaxStatus('success', 'Meta fields updated.', $dataArray);

It generates a blank value for $post_ID in the console.

{"status":"success","message":"Meta fields updated.","data":{"":false}}

But In the single.php page, the $post_ID is found and the echo works

<?php 
global $wp_query; 
$post = $wp_query->get_queried_object();
$post_ID = $post->ID; 
?>                              <div id="test"><?php echo($post_ID); ?></div>

Javascript is here

jQuery(document).ready(function($) {
    var response;
    $('#checkbox').on('submit',function(e) {

    e.preventDefault();

    $.post( checkbox.ajaxurl, {
          action : 'submit_checkboxes',
          nonce : checkbox.nonce,
          post : $(this).serialize()
      },
      function(response) {
          console.log(response);
          responseSuccess(response);
      });

    return false;
    });
    function responseSuccess(data) {

    response = JSON.parse(data);

    if(response.status === 'success') {
      $('#checkbox-message').text(response.message);
    } else {
      $('#checkbox-message').text(response.message);
    }
    }   });

This is the ajax

function ajaxStatus($status, $message, $data = NULL) {

  $response = array (
    'status' => $status,
    'message' => $message,
    'data' => $data
    //'data' => $post_ID
    );
  $output = json_encode($response);

  exit($output);

}
  • Where is the javascript code that makes the ajax request? That's where you need to get and pass the post ID. – Milo Jan 15 '18 at 4:47
  • can you share your ajax call too? – tpaksu Jan 15 '18 at 5:03
  • I just added them to the original post. – DonChi Jan 15 '18 at 5:39
1

I may be missing something in your code; but:

  1. you need to "move" the content of your test div to a JS variable that can then be used in your Ajax (or use the div value directly in your $.post.

    e.g. var postID = $('#test').val();

  2. then use it to pass the needed info to your server

    $.post( checkbox.ajaxurl, {
       action : 'submit_checkboxes',
       nonce : checkbox.nonce,
       thispost : postID,
       post : $(this).serialize()
      },
      function(response) {whatever... }
    );
    
  3. in your PHP use say intval($_POST['thispost']) to obtain the post id and use accordingly.

Note: not tested & I tend to use jQuery.ajax with a data string of values instead of jQuery.post

| improve this answer | |
  • I really appreciate all your help. It helped point me to the/an answer! Though I didn't use the code directly, it led me to logically follow the flow of information back to the form from which I was pulling. Instead of having the form embedded in the post pages themselves, I put the form in the single.php file so it was universally applied to all posts. Then I simply applied a <?php echo ($post_ID) ?> into to the "for", "ID", and "name" fields of the inputs. Thanks Again! – DonChi Jan 15 '18 at 12:13
  • Glad it helped & its good you posted your end solution - it may be of use to others wishing to apply something to all posts. – scytale Jan 15 '18 at 12:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.