Page 1 of 3

Adding multiple items (from checkboxes) to a database array

Posted: Wed Mar 13, 2013 10:48 pm
by sam.wiltshire

Hey there

I am trying to add multiple items from a check box to a database array. The intention is that the user can pick and choose their preferences and all of these will be saved in the same array (ideally this will be very scalable as I intend to add at least a hundred checkboxes).

I have noticed two different posts which sort of seem like they may be in the right direction: https://getsatisfaction.com/tiggzi/to... and https://getsatisfaction.com/tiggzi/to... however they only seem to be working for when items are added to the array one at a time and not at the same time.

Any help that you can provide will be much appreciated!

Also, FYI I am fairly new to javascript and tiggzi so please err on the side of more detail vs less if possible. Thank you in advance!


Adding multiple items (from checkboxes) to a database array

Posted: Thu Mar 14, 2013 6:49 am
by Maryna Brodina

Hello! Could you clarify please where do you want to save items? In localStorage or to DB?


Adding multiple items (from checkboxes) to a database array

Posted: Thu Mar 14, 2013 10:49 am
by sam.wiltshire

Hey Marina,

Preferably to DB for now, but it would probably be useful to save to local storage as well for a few functionality ideas that I have coming up.

Sam


Adding multiple items (from checkboxes) to a database array

Posted: Thu Mar 14, 2013 10:23 pm
by Kateryna Grynko

Hi Sam,

You would need JavaScript code to do this:

  • read all the values that need to be saved in database;

  • generate JavaScript array;

  • save an array in database using Rest Service.

    What data should be saved in array: selected items in Checkbox Group or other?


Adding multiple items (from checkboxes) to a database array

Posted: Thu Mar 14, 2013 11:43 pm
by sam.wiltshire

Hi Katya

Thanks for this.

Ok cool. I would assume that we would use a local storage variable to store the array.

And yes, I want to save the value of the checkbox if it is checked. I.e. if we have:

[ ] blue
[x] red
[x] black
[ ] yellow
[x] green

Would be then stored as an array: colours["red", "black", "green"]

Also, if you have any suggestions on where to start, resource-wise for JS for this then that would be much appreciated.

Sam


Adding multiple items (from checkboxes) to a database array

Posted: Fri Mar 15, 2013 9:02 am
by Maryna Brodina

Hello! You can search for jQuery Mobile documentation. For example to find component by name you can use:

codeTiggzi('checkboxgroup')/code

to find check box items:

codeTiggzi('checkboxgroup').find('input[type=checkbox]')/code

to select check box items which are checked:

codeTiggzi('checkboxgroup').find('input[type=checkbox]').filter(function(i, elem) {return $(elem).prop("checked");} )/code


Adding multiple items (from checkboxes) to a database array

Posted: Sat Mar 16, 2013 12:16 am
by sam.wiltshire

Thanks Marina, I'll give it a shot over the weekend/Monday (It is St Paddy's day this weekend after all!)


Adding multiple items (from checkboxes) to a database array

Posted: Wed Mar 20, 2013 12:01 am
by sam.wiltshire

Hi Marina

I have been trying to do this and, along with a few Stack Overflow articles I have managed to get the selected check box values into a comma separated alert.

However, I have been trying to follow one of your other threads to try and get it into a local storage variable with no luck. The thread is here - https://getsatisfaction.com/tiggzi/to...

My code looks like the following:

In a separate JS file:
precode
function save(item, arrName) {
var arr = JSON.parse(localStorage.getItem(arrName));
if(arr == null) {
arr = new Array();
}
arr.push(item);
localStorage.setItem(arrName, JSON.stringify(arr))
}

function myload(arrName) {
var arr = JSON.parse(localStorage.getItem(arrName));
if(arr == null) {
arr = new Array();
}
return arr;
}
/code/pre

And what I am calling when the user clicks on the submit button after selecting their checkboxes:
precode
var userTeams = [];
$(document).ready(function () {
var selectedValues="&quot
$checkedCheckboxes = $("input:checkbox[name=basketballNBA_checkboxes]:checked");
$checkedCheckboxes.each(function () {
//save( $(this).val() , 'userTeams');
selectedValues += $(this).val() +",&quot
});
alert(selectedValues);
});
/code/pre

In Chrome dev tools it appears that I am getting the following error on the first line in the seperate JS files - Uncaught Syntaxerror: unexpected token u.

Any help would be greatly appreciated!


Adding multiple items (from checkboxes) to a database array

Posted: Wed Mar 20, 2013 6:48 am
by Kateryna Grynko

Hello,
I'll take a look.


Adding multiple items (from checkboxes) to a database array

Posted: Wed Mar 20, 2013 1:51 pm
by Kateryna Grynko

I cannot reproduce this.
Could you please share your app with a href="mailto:support@tiggzi.com" rel="nofollow"support@tiggzi.com/a and provide exact steps to reproduce?