1

I have custom post loop, and for each product I have form. I'd like to store data in array for each click submit button in each product. E.x. Product A with Form and some values (price,quantity) click submit, and I have array with values in my function. Product B with Form and some values (price,quantity) click submit, and I have array with values from values from BV product in my function, but override old values from product A.

ajax:

jQuery(function ($) {
    $(document).ready(function () {


        jQuery("form[name='form_add_to_cart']").on('submit', function (e) {
            e.preventDefault();
            jQuery.ajax({
                url: my_ajax_object.ajax_url,
                method: 'post',
                data: $(this).serialize(),
                success: function (response) {
                    $('.response').html(response);
                    //console.log(formData);
                },
                fail: function (err) {
                    alert("There was an error: " + err);
                }
            });
            return false;
        });
    });
});

functions.php

add_action('wp_ajax_send_form', 'send_form');
add_action('wp_ajax_nopriv_send_form', 'send_form');
function send_form()
{
    $data_array();
    if (isset($_POST['action'])) {
        $data = $_POST;
    }

    return $data;

}

Don't know how to store values from each form... whole form : https://pastebin.com/YJXTjGjJ screenshot for clarification: https://ibb.co/LCvgQmP

2
  • 1. Actually, what will your send_form() really do? Don't tell me you simply wanted to return the submitted data back to the form? Because if so, there's no point submitting the form to the server. 2. "click submit, and I have array with values in my function" - you mean, your PHP function? 3. Your JS already have that $('.response').html(response) which I supposed would change the data in the right sidebar, so what kind of answer/solution you're looking for? And I know you said, "store values from each form", but store to where? The $data array?
    – Sally CJ
    Sep 24 at 8:12
  • My point is to transfer the values ​​of each form from left to right, like a shopping cart. So when someone creates their basket (right side) with things (left side), my basket will also be the form that I will send the data to the database. I just want to get the values ​​of the things on the left, on the right.
    – NCTI
    Sep 24 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.