11

I have two forms for which I want to send the data through jQuery Ajax call. I managed to send it successfully for one form but I am not able to send the data from both of them through the same Ajax call.

My forms are:

<form id="filter-group1"  method="post" name="filtergroup1">
<input type="checkbox" name="colour1" value="Value1" onclick="filterBy()" /><label>Option 1</label>
<input type="checkbox" name="colour2" value="Value2" onclick="filterBy()" /><label>Option 2</label>
<input type="checkbox" name="colour3" value="Value3" onclick="filterBy()" /><label>Option 3</label>
</form>
<form id="filter-group2" method="post" name="filtergroup2">
<input type="checkbox" name="size1" value="Value1" onclick="filterBy()" /><label>Option 1</label>
<input type="checkbox" name="size2" value="Value2" onclick="filterBy()" /><label>Option 2</label>
<input type="checkbox" name="size3" value="Value3" onclick="filterBy()" /><label>Option 3</label>
</form>

And the function is:

function filterBy() { 
    var fgroup1 = document.filtergroup1;
    var fgroup2 = document.filtergroup2;
    var dataString1 = $(fgroup1).serialize();
    var dataString2 = $(fgroup2).serialize();
    var filterdata = [];
    filterdata.push(dataString1,dataString2);
    $.ajax( {
        type: 'POST',
        url: 'filter.php',
        data: filterdata,
        success: function(data) {
            console.log(data);
            $('#message').html(data);
        }
    });
}

I have this in the php file:

echo var_export($_POST);

The function works fine for one form if I replace data: with

data: dataString1,

I am trying to achieve the same result with the data from both forms but I don't want to use a different function for each form.

I appreciate any help. Thank you.

2 Answers 2

23

jQuery's serialize() method concatenates your input values with an '&' symbol - therefore when you are pushing two serialized form data, you are creating an array and not concatenating the values in two forms with '&' (which is the one that will be parsed correctly). You can either: (1) concatenate the items in the array into a string with '&' or (2) use $("#form1, #form2").serialize() to do it:

function filterBy() { 
    // Construct data string
    var dataString = $("#filter-group1, #filter-group2").serialize();

    // Log in console so you can see the final serialized data sent to AJAX
    console.log(dataString);

    // Do AJAX
    $.ajax( {
        type: 'POST',
        url: 'filter.php',
        data: dataString,
        success: function(data) {
            console.log(data);
            $('#message').html(data);
        }
    });
}

[Edit]: On a side note, I highly discourage using inline JavaScript. You should separate content from function. Instead, use the .click() function, like:

$("form input[type='checkbox']").click(function() {
    var dataString = $("#filter-group1, #filter-group2").serialize();
    // (and more...)
});

I also don't exactly understand the reasoning behind using two separate forms...

Sign up to request clarification or add additional context in comments.

1 Comment

@Terry The two separate forms are in 2 different parts of the website, I just pasted them here like that for readability. Thank you for the tip regarding inline JavaScript, I will use it like you suggested.
0

You're using a function call in your filtergroup2 form that doesn't exist.

filterBy2() I assume needs to be filterBy()

3 Comments

Yes, I've corrected that now, I was trying it with a second function but forgot to make the correction here.
Well, the JavaScript seems to work as expected as per this fiddle jsfiddle.net/bambattajb/ny6nQ. What are the results of print_r($_POST['data']); in your PHP file?
The results are: array ( 'undefined' => 'undefined', )

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.