0

I'm trying to build a form in the backoffice to save some data in a custom table. The form is a list of checkbox, in witch the value is linked to a selected user.

So you have to select a user in a dropdown list, hit filter and then an ajax call is made to load the real form with checked value when checked before. This parts works well, I have my pre checked values. On submit, the form needs to save the selected user_id and also the checked checkboxs in order to save each checked value with the appropriate user_id.

The troubles comes when I try to save the form. First problem is when I do die(var_dump($_POST)); at the beginning of my save function, I get the selected user_id as expected but I don't get all of the checked checkboxs and instead I get only the last checked value (exemple I check 4 checkboxs I'm expecting 4 Ids in $_Post, but i Only get the last one). Weird thing is that if I look at the payload in network tabs I see all of my data but somehow they dont endup in my $_Post.

Second trouble is that before saving I need to check wich value is already in the DB so I can save new ones and delete the uncheked values. How can I do that ? I've tried a few things but not sure how to tackle this. And lastly my saving loop does not work, Wordpress always return "WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1] INSERT INTO wp_mytable( store_id,data_id) VALUES;" Any one can help me figure this out ?

EDIT Thanks to alexcool88 I now have all of my checked checkboxs Id in $_Post

Now I'm still wondering how to save these, and at the same time remove from db the unchecked id if they were checked before. Should I delete every entries with the corrersponding store_id ? Or should I compare which is saved dans add remove data depending onc hecked values ?

brand.php

 <div class="wrap">

     <div class="brand">
         <div class="brand_dropdown">
             <p class="brand_text">Form </p>

             <form method="POST"  action="<?php echo site_url() ?>/wp-admin/admin-ajax.php">
                 <div class="select">
                     <input type="hidden" name="action" value="filterBrandRestrict">

                     <select name="retailerfilterwatch">
                         <option value="">Select retailer...</option>
                         <?php

                            $posts = new WP_Query(array('post_type' => 'retailer'));
                            while ($posts->have_posts()) : $posts->the_post();
                                wp_reset_postdata(); ?>
                             <option id="selection" value="<?php echo get_the_ID(); ?>"><?php echo get_the_title(); ?></option>

                         <?php endwhile; ?>
                     </select>


                     <button id="filter_btn">Apply filter</button>

                 </div>
             </form>
         </div>
         <div class="brand__checkbox" id="responseBrand">




         </div>
     </div>
Form.php

add_action('wp_ajax_filterBrandRestrict', 'filter_brand_retailer');
add_action('wp_ajax_nopriv_filterBrandRestrict', 'filter_brand_retailer');

function filter_brand_retailer()
{

    $store_id = $_POST['retailerfilter'];
    global $wpdb;
    //var_dump($_POST);

    $query = $wpdb->prepare("SELECT data_id FROM `mytable` WHERE `store_id` = '%d'", $store_id);

    $results = $wpdb->get_results($query, OBJECT_K);

    $Brand = new WP_Query(array('post_type' => 'watch_brand'));
?>
    <?php
    if ($Brand->have_posts()) : ?>
        <div class="control-group legend_brand">
            <form method="post" action="<?php echo site_url() ?>/wp-admin/admin-post.php" contentType: false,>
                <!-- Store id -->
                <input type="hidden" name="store_ids" value="<?php echo $store_id ?>">
                <input type="hidden" name="action" value="saveBrand">
                <!--  loop on brand to create checkboxes -->
                <?php while ($watchBrand->have_posts()) : $watchBrand->the_post(); ?>
                    <label class="control control-checkbox">
                        <?php echo get_the_title(); ?>

                        <input name="data_ids" value="<?php echo get_the_ID(); ?>" <?= isset($results[get_the_ID()]->data_id)  == get_the_ID() ? 'checked' : '' ?> type="checkbox" />

                        <div class="control_indicator"></div>
                    </label>
                <?php endwhile; ?>
                <div class="submit_default">
                    <button name="Submit" class="submit_button" type="submit">Envoyer</button>
                </div>
            </form>
        </div>
<?php
        wp_reset_postdata();
    else :
        echo 'No posts found';
    endif;

    die();
}

Thanks

1 Answer 1

0

You must define the checkbox to store array in order to get all the selected values. Change your input name from "data-ids" to "data-ids[]".

<input name="data_ids[]" value="<?php echo get_the_ID(); ?>" <?= isset($results[get_the_ID()]->data_id)  == get_the_ID() ? 'checked' : '' ?> type="checkbox" />
2
  • Hi, axcool88, thanks that solved my first problem, now I get every checked checkboxes Id. How would you save the checkboxes id ? Since there won't be more than 20 checkboxes, I tought about deleting every entries with the user_id and then insert the checkboxes values, might be faster to execute for the server rather than checking wich is checked and update accordingliny and delete the unckecd value
    – Francis
    Commented Apr 28, 2023 at 10:04
  • Hi @Francis, your thought is right, deleting all entries and saving the entire checked values again is the easiest method.
    – axcool88
    Commented May 3, 2023 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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