0

I have 2 input fields like so

<input type="text" name="typeDetails[games]"/>
<input type="text" name="typeDetails[art]"/>

I want to submit via AJAX, but I'm unsure how to exactly send this data. I know in my controller that I can validate the typeDetails array like typeDetails.games or typeDetails.art, but I'm unsure how to send it.

Here's what my JS looks like currently.

var data = { 'typeDetails[]' : []};

$("input[name='typeDetails[games]']").each(function() {
    data['typeDetails[games]'].push($(this).val());
}); 

$("input[name='typeDetails[art]']").each(function() {
    data['typeDetails[art]'].push($(this).val());
});

The error I'm getting is "Cannot read property 'push' of undefined".

Thanks!

2 Answers 2

1

'typeDetails[games]' and 'typeDetails[art]' are not the keys of data.

Declare data like this

var data = { 'typeDetails[games]' : [],  'typeDetails[art]' : []};
Sign up to request clarification or add additional context in comments.

Comments

0

Try using like

var data = new Array();
$("input[name=typeDetails]").each(function() {
    data.push($(this).val());
});

Comments

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.