How can I pass an array and another var in jquery?

This works:

`function add(){`
`$('#add_sleep_form').on("submit", function(event){`
`event.preventDefault();`

`if($('#sleep').val() == "") {`
`alert("Sleep Time is required");`
`} else {`
`$.ajax({`
`type:"POST",`
`data:$('#add_sleep_form').serialize(),`
`url:"insert_sleep.php",`
`success:function(data){`
`start();`
`$('#add_sleep').modal('hide');`
`reset(add_sleep_form);`
`alert("Record Added Successfully");`
`}`
`});`
`}`
`});`
`}`

But add another var doesn’t, I was wondering maybe I can send an array and a separate var

`function add(){`
`$('#add_sleep_form').on("submit", function(event){`
`event.preventDefault();`
`var page = '<?php echo $page; ?>'; <======== php var
`if($('#sleep').val() == "") {`
`alert("Sleep Time is required");`
`} else {`
`$.ajax({`
`type:"POST",`
`data:{$('#add_sleep_form').serialize(), page:page}, <====== an array and a var
`url:"insert_sleep.php",`
`success:function(data){`
`start();`
`$('#add_sleep').modal('hide');`
`reset(add_sleep_form);`
`alert("Record Added Successfully");`
`}`
`});`
`}`
`});`
`}`

Hello, I dont know a lot about JQuery but when you start sending and/or linking code on SPA`s it can get complicated.

Global web icon


Stack Overflow

https://stackoverflow.com/questions/1286084

javascript - Pushing value of Var into an Array - Stack …

jQuery is not the same as an array. If you want to append something at the end of a jQuery object, use: $(‘#fruit’).val($(‘#fruit’).val()+veggies); In your case, fruitvegbasket is a string that contains the current value of #fruit, not an array.

Code sample

var veggies = “carrot”;

var fruitvegbasket = ;

fruitvegbasket.push(veggies);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.