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 8587095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:28:58+00:00 2026-06-11T22:28:58+00:00

I am still learning how to use AJAX so would display a novice code

  • 0

I am still learning how to use AJAX so would display a novice code here…

I got this div (which repeats itself as a list of checkbox):

<div class="updateTask fs11">
  <input type="checkbox" name="taskStatusRadio" id="taskStatus" value="<?php echo $taskId; ?>" <?php echo $done; ?> > 
  <?php _e('Task Done', 'sagive'); ?>
</div>

Which activates this:

jQuery(function($){
    $('.updateTask').click(function () {

        $.post(ajax_object.ajaxurl, {
            action: 'action_update_task',
            task_id: $("input[name=taskStatusRadio]:checked").map(function () {return this.value;}).get()

        }, function(data) {

            // USE DATA RETURNED //////////
            var $response   = $(data);
            var message     = $response.filter('div#message').html();
            var taskid      = $response.filter('div#taskid').html();        

            // SUCCESS RESPOND //////////
            setTimeout(function(){
                $('#success ul li').append(message + taskid);
                $('#success').fadeIn();
                $('#success').css("display", "block");              
            }, 1000);   

            setTimeout(function(){
                $('#success ul li').empty();
                $('#success').fadeIn();
                $('#success').css("display", "none");               
            }, 30000);


            hideTask = "#" + taskid;
            $(hideTask).hide("slow");

            hideTask = '';

        });
    });
});

And uses this php file:

wp_enqueue_script( 'ajax-update-task', get_stylesheet_directory_uri().'/ajaxLoops/ajax-update_task.js', array('jquery'), 1.0 ); // jQuery will be included automatically
wp_localize_script( 'ajax-update-task', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl

add_action( 'wp_ajax_action_update_task', 'ajax_update_task' ); // ajax for logged in users
function ajax_update_task() {

    global $current_user;
    get_currentuserinfo();
    $task_user = $current_user->display_name;
    if($taskUser == '') {$taskUser = $current_user->user_login;}

    $task_id = $_POST["task_id"];
    $task_id = $task_id[0];
    $task_status = 'done';
    $task_title = get_the_title($task_id);
    $task_title = mb_substr($task_title, 0 ,35).'...';

    update_post_meta($task_id, '_sagive_task_radio_selector', $task_status);
    update_post_meta($task_id, '_sagive_task_user_changed', $task_user);

    echo '<div id="message">'.__('The task: ', 'sagive').$task_title.__('Was Marked Completed!', 'sagive').'</div>';
    echo '<div id="taskid">'.$task_id.'</div>';

    die(); // stop executing script
}

It all works fine the first time. But the second checkbox I mark after the first one disappears as expected does nothing. It doesn’t activate the php script and doesn’t return a response.

Since I’m still new using AJAX, I would appreciate an example using my code or a good example with explanation.

Revision 1:
This is the structure of the page where the checkboxes are at
enter image description here

  • 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-11T22:28:59+00:00Added an answer on June 11, 2026 at 10:28 pm

    I think your problem comes from your selector :

    $("input[name=taskStatusRadio]:checked").map(function () {return this.value;}).get();
    

    which returns all the taskStatusRadio input checked and not just the one you click.

    Your php script receive all the taskid checked in an array an pick the first one to treat it and send a response.

    So the first time, it’s ok, you just have one checkbox checked. But when you check a second checkbox, all checked taskid will be send and only the $_POST["task_id"][0] will be treated.

    Same response from your php script and no change in the front view.

    So, i think, you just have to modify a little bit your code, to post only taskid of the checkbox you click on it.

    jQuery(function($) {
        // we listen only the checkbox, not the div click action
        $(':checkbox', '.updateTask').click(function () {
            // if the checkbox is checked
            if ($(this).attr('checked') == "checked") {
    
                $.post(ajax_object.ajaxurl, {
                      action: 'action_update_task',
                      task_id: $(this).val() }, 
                      function(data) {
    
                          // SUCCESS RESPOND //////////
                          setTimeout(function() {
                              $('#success ul li').append( $(data).html());
                              $('#success').fadeIn();
                              $('#success').css("display", "block");              
                              }, 1000);   
    
                          setTimeout(function() {
                              $('#success ul li').empty();
                              $('#success').fadeIn();
                              $('#success').css("display", "none");               
                              }, 30000);
    
                          // we hide the checkbox
                          $(this).hide("slow");
                 });
             }
         });
     });
    

    And because of this change in the front javascript, we have to simplify your php script like this :

    wp_enqueue_script( 'ajax-update-task', get_stylesheet_directory_uri().'/ajaxLoops/ajax-update_task.js', array('jquery'), 1.0 ); // jQuery will be included automatically
    wp_localize_script( 'ajax-update-task', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl
    
    add_action( 'wp_ajax_action_update_task', 'ajax_update_task' ); // ajax for logged in users
    function ajax_update_task() {
    
        global $current_user;
        get_currentuserinfo();
        $task_user = $current_user->display_name;
        if($taskUser == '') {$taskUser = $current_user->user_login;}
    
        $task_id = $_POST["task_id"];
        $task_status = 'done';
        $task_title = get_the_title($task_id);
        $task_title = mb_substr($task_title, 0 ,35).'...';
    
        update_post_meta($task_id, '_sagive_task_radio_selector', $task_status);
        update_post_meta($task_id, '_sagive_task_user_changed', $task_user);
    
        //  Note : now we send the message directly well-formed with the task_id
        echo __('The task: ', 'sagive').$task_title.__('Was Marked   Completed!', 'sagive'). $task_id;
    
        die(); // stop executing script
    }
    

    I hope my answer will solve your problem 😉

    ps: i apologize for my poor english…

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

Sidebar

Related Questions

Still learning Objective-C / iPhone SDK here. I think I know why this wasn't
Still learning JSP Web Applications here. I have been doing this for a while
Hi sorry still learning here and slow to learning code arguments. Just wondering could
I'm still learning how to use Sizzle selector. So far I know this: Sizzle('#blah')
I'm still learning how to use SQL, and I need some help creating this
Still learning WPF....thanks for any help. Is there any way to Refactor this: <ListBox
Still learning CSS so pardon if this is obvious - I am building a
I'm still learning js and trying out the Webstorm IDE, which seems sweet (including
how would i get a random rowcount using PDO? i'm still learning how to
I am still learning SQL so this may seem a very odd question, but

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.