Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  166] [ 3]  / answers: 1 / hits: 20067  / 10 Years ago, wed, march 5, 2014, 12:00:00

I have the following snippet where I am serializing form data and posting it via ajax. I have come across a situation where I need to add additional data. In this case I need to add a comma separated array called 'selectedHours'. Is this possible?



I am creating 'selectedHours' through as shown below where it creates an array of list items with the class 'hour-selected'. There are no form values, inputs, etc used in this aspect.



var selectedHours = [];
$('.hour-selected').each(function(k,v) {
selectedHours.push($(v).text());
});

$.ajax({
type: 'post',
url: '/process/somepage.php',
data: $form.serialize(),
dataType : 'json'
}).done(function (response) {
... and so on...

More From » jquery

 Answers
33

try this:



$.ajax({ 
type: 'post',
url: '/process/somepage.php',
data: $form.serialize() + '&hours=' + JSON.stringify(selectedHours),
dataType : 'json'
}).done(function (response) {
... and so on...


data sended are just a URL encoded string. You can append other value with a simple concatenation.


[#72133] Tuesday, March 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
berenices

Total Points: 104
Total Questions: 106
Total Answers: 106

Location: Spain
Member since Thu, Dec 23, 2021
2 Years ago
;