Home > Net >  What's the problem with my Wordpress ajax request
What's the problem with my Wordpress ajax request

Time:01-31

I want to add have a form in my Wordpress dashboard that work with ajax but my request response is 400 Bad request

add_action('admin_footer', 'vitrin_admin_ajax');
        function vitrin_admin_ajax() {
        ?>
            <script>
                (function($) {
                    $('.vitrin-private-message-form').submit(function(event) {
                        event.preventDefault();
                        jQuery.ajax({
                            url: '<?php echo admin_url('admin-ajax.php'); ?>',
                            type: 'post',
                            data: {
                                action: 'submit_vitrin_pm',
                                ppmcontent: jQuery('#pmcontent').val(),
                                ppmusers: jQuery('#pmusers').val(),
                            },
                            success: function(data) {
                                // jQuery('#datafetch').html(data);
                                alert(data)
                            }
                        });
                    })
                })(jQuery);
            </script>
        <?php
        }
        ?>

I also add:

add_action('wp_ajax_submit_vitrin_pm', 'submit_vitrin_pm');
add_action('wp_ajax_nopriv_submit_vitrin_pm', 'submit_vitrin_pm');

and

function submit_vitrin_pm() {
    echo 'ssss';
    die();
}

CodePudding user response:

I checked, your ajax response is working, you didn't share your html form, i added a form for check.

add_action('admin_footer', 'vitrin_admin_ajax');
function vitrin_admin_ajax() {
?>

      <form action="#" method="POST" >
        <div id="name-group" >
          <label for="name">Name</label>
          <input
            type="text"
            
            id="pmcontent"
            name="name"
            placeholder="Full Name"
          />
        </div>

        <div id="email-group" >
          <label for="email">Email</label>
          <input
            type="text"
            
            id="pmusers"
            name="email"
            placeholder="[email protected]"
          />
        </div>

        <button type="submit" >
          Submit
        </button>
      </form>
    <script>
        (function($) {
            $('.vitrin-private-message-form').submit(function(event) {
                event.preventDefault();
                console.log('working')
                jQuery.ajax({
                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    type: 'post',
                    data: {
                        action: 'submit_vitrin_pm',
                        ppmcontent: jQuery('#pmcontent').val(),
                        ppmusers: jQuery('#pmusers').val(),
                    },
                    success: function(data) {
                        // jQuery('#datafetch').html(data);
                        alert(data)
                    }
                });
            })
        })(jQuery);
    </script>
<?php
}


add_action('wp_ajax_submit_vitrin_pm', 'submit_vitrin_pm');
add_action('wp_ajax_nopriv_submit_vitrin_pm', 'submit_vitrin_pm');

function submit_vitrin_pm() {
    echo 'ssss';
    die();
}
  • Related