2

I have the following code, and I want to pass the value[i] into a data string in a json ajax post submit, Basically what I am trying to do is grab all the checked values in a form and put them into a existing json data string in ajax.

code I have so far

 var values = $('input:checked').map(function () {
    return this.value;
}).get(); // ["18", "55", "10"]
//console.log(values);

var length = values.length;

for (i = 0; i < length; i++){
    console.log(values[i]);


}



 $.ajax({
type:"POST"
,url : $(form).prop('action')
,data:{
name:getValue($('label.name input'))
,email:getValue($('label.email input'))
,phone:getValue($('label.phone input'))
,fax:getValue($('label.fax input'))
,state:getValue($('label.state input'))
,message:getValue($('label.message textarea'))
,owner_email:opt.ownerEmail||'#'
,stripHTML:opt.stripHTML
}
1
  • Are you wanting to add a key for every value? Or are you good just adding an array to the DATA argument? Commented Nov 28, 2014 at 2:24

2 Answers 2

2

You can create your object first, then pass it like this:

  var obj= new Object();
  obj.name=getValue($('label.name input'));
  ...
  obj.values:values

etc

$.ajax({
type:"POST"
,url : "YOUR URL"
,data: JSON.Stringify(obj)
 });

THAT'S IT Good Luck

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

Comments

1

Set a variable data, add a values key to it, then send it in your AJAX request.

var values = $('input:checked').map(function () {
    return this.value;
}).get(); // ["18", "55", "10"]

var data = {
    name       : getValue($('label.name input')),
    email      : getValue($('label.email input')),
    phone      : getValue($('label.phone input')),
    fax        : getValue($('label.fax input')),
    state      : getValue($('label.state input')),
    message    : getValue($('label.message textarea')),
    owner_email: opt.ownerEmail||'#',
    stripHTML  : opt.stripHTML,
    values     : values
}

$.ajax({
    type: "POST",
    url : $(form).prop('action'),
    data: data
});

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.