Ecommerce Shopify WordPress Discussion

POST http://domain/wp-admin/admin-ajax.php 400 (Bad Request)

I've set up a form that sends data to a database table using AJAX, but I'm getting a "400 Bad Request" error when submitting the form. Here's my setup: I have a form in my WordPress site that collects user information (first name, last name). When the form is submitted, the data is sent via AJAX to a PHP function (add_address) defined in my child theme's functions.php file. The PHP function is supposed to sanitize and insert the data into a custom database table (pl_addresses). Here's the relevant code: add_action('wp_ajax_add_address', 'add_address'); add_action('wp_ajax_nopriv_add_address', 'add_address'); function add_address() { global $wpdb; // Sanitize input data $name = sanitize_text_field($_POST['address-firstname']); $lastname = sanitize_text_field($_POST['address-lastname']); // Prepare and execute SQL query using prepared statements $wpdb->insert( 'pl_addresses', array( 'name' => $name, 'lastname' => $lastname, ), array( '%s', '%s' ) ); } This is JavaScript code in .js file: jQuery('#add_address_form').on('submit', function () { var form_data = jQuery(this).serializeArray(); form_data.push({ "name": "security", "value": ajax_nonce }); console.log('Form data:', form_data); console.log('AJAX URL:', ajax_url); jQuery.ajax({ url: ajax_url, type: 'post', data: form_data, success: function (response) { console.log('AJAX Response:', response); alert(response); }, fail: function (err) { console.error("There was an error: " + err); alert("There was an error: " + err); } }); return false; }); This HTML form: <form id="add_address_form" method="post" action="" class="form-horizontal form-control-borderless display-none"> <h4 class="sub-header"><i class="gi gi-home"></i><span>Pridėti pristatymo adresą</span></h4> <div class="form-group col-md-12"> <div class=" col-md-6 col-xs-12"> <div class="input-group"> <span class="input-group-addon"><i class="gi gi-user"></i></span> <input type="text" id="address-firstname" name="address-firstname" class="form-control input-lg" placeholder="Vardas *" required> </div> </div> <div class=" col-md-6 col-xs-12"> <div class="input-group"> <span class="input-group-addon"><i class="gi gi-user"></i></span> <input type="text" id="address-lastname" name="address-lastname" class="form-control input-lg" placeholder="Pavardė *" required> </div> </div> </div> <div class="form-group form-actions col-md-12"> <div class="col-xs-6 text-right" style="width:auto;"> <button type="submit" class="btn btn-sm btn-success"><i class="fa fa-plus"></i> Užregistruoti</button> </div> </div> </form> Also have this code in head tag: <script type="text/javascript"> var ajax_url = '<?php echo admin_url("admin-ajax.php"); ?>'; var ajax_nonce = '<?php echo wp_create_nonce("secure_nonce_name"); ?>'; </script> And i get such error: I've confirmed that the AJAX URL and nonce are correctly set. However, when I submit the form, I receive a "400 Bad Request" error in the console, and the AJAX request fails. What could be causing this issue? Any suggestions for troubleshooting or debugging would be greatly appreciated.
Try this code. You missed action: 'add_address' jQuery('#add_address_form').on('submit', function () { var form_data = jQuery(this).serializeArray(); form_data.push({ "name": "security", "value": ajax_nonce }); console.log('Form data:', form_data); console.log('AJAX URL:', ajax_url); jQuery.ajax({ url: ajax_url, type: 'post', data: { action: 'add_address', formdata: form_data }, success: function (response) { console.log('AJAX Response:', response); alert(response); }, fail: function (err) { console.error("There was an error: " + err); alert("There was an error: " + err); } }); return false; });

February 26, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.