Wednesday, July 3, 2024
HomeWordPress DevelopmentAdmin ajax error 400 when passing choose worth to populate one other...

Admin ajax error 400 when passing choose worth to populate one other choose


That is my first try to put in writing a plugin and I get an error 400 when making an attempt to make use of admin-ajax.php. I’ve appeared by some matters each right here and within the WordPress assist discussion board, however can not determine it out.

What I’m making an attempt to do is dependant drop-down choose. I.e. you selected an possibility from choose #1, then it needs to be handed by way of ajax to populate the second dropdown. To make it easy (for now), every thing is on my “frontend” file.

Right here is my code:


        <div class="row">
            <choose class="cat-select" identify="lvl1" id="lvl1">
                <possibility worth="">-- choose class --</possibility>
                <?php
                foreach ($this->getTopLevelCategories() as $class) {
                    print '<possibility worth="' . $category->term_id . '">' . $category->identify . ' (' . $category->rely . ')</possibility>';
                }
                ?>
            </choose>
        </div>

        <script kind="textual content/javascript">
            jQuery(doc).prepared(perform () {
                jQuery('#lvl1').change(perform () {
                    var lvl1Val = jQuery('#lvl1').val();
                    jQuery('#lvl2').empty();
                    jQuery.ajax({
                        url: "<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",
                        technique: 'POST',
                        information: {
                            'motion':'getLVl2',
                            'lvl1' : doc.getElementById('lvl1').worth
                        },
                        success: perform(response, standing) {
                            jQuery('#lvl2').append(response);
                            alert(response);
                            console.log(response);
                        },
                        error: perform(errorThrown){
                            console.log(errorThrown);
                        }
                    });
                });
            });
        </script>

        <div class="row">
            <choose class="cat-select" identify="lvl2" id="lvl2">
                <possibility worth="" disabled chosen>-- choose class lvl2 --</possibility>
            </choose>
        </div>

        <?php

        perform getLVl2()
        {
            if (isset($_POST['lvl1'])) :

                $dad or mum = $_POST['lvl1'];

                $possibility = '<possibility worth="' . $dad or mum . '">';
                $possibility .= $dad or mum;
                $possibility .= '</possibility>';

                echo $possibility;

                wp_die();

            endif;
        }

        add_action('wp_ajax_getLVl2', 'getLVl2');
        add_action('wp_ajax_nopriv_getLVl2', 'getLVl2');
        ?>

Be aware:

  1. The code within the perform will likely be modified. What I’m making an attempt to do is output the worth of the choice chosen from the primary dropdown simply to see that it really works.
  2. The perform and the script will likely be moved to their locations to “maintain the code clear”
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments