Forum Moderators: open

Message Too Old, No Replies

Help requesting for JQuery loop for processing form checkboxes

         

bazil greyson

5:36 pm on Aug 17, 2009 (gmt 0)

10+ Year Member



Here I sit, nursing another headache as I try to continue to add functionality to Jomsocial by bridging it and Agora Forums while adding my own content and functionality as well.

This is what is stumping me. I am working on making Jomsocial's group categories a checkbox input and allow users to select more than one category. The output is simple enough for me to parse, and I have even tracked down where in three different files I must make alterations. It is on one function that I have been unable to advance.

Here is what I have...

The loop for the checkboxes:
------------ CODE ---------
<div class="cparam group-geektags">
<span class="cinput">
<div class="clabel"><?php echo JText::_('CC GROUP INFO CATEGORY'); ?>:</div>
<?php if( $categories ) { ?>
<?php foreach( $categories as $category ) {
$tagcount++;
$tag = "geektag" . $tagcount;
?>
<input type="checkbox" name="<?php echo $tag;?>" value="<?php echo $category->id;?>¦"><?php echo $category->name;?><br>
<?php } ?>
<?php
} else {
echo JText::_('CC NO GROUP CATEGORIES');
} ?>
</span>
</div>
------------ CODE ---------

Upon hitting the "save" button in the editor, all fields are sent to a javascript file that processes them, rather than a post / get style. I didn't write it this way, but rewriting it is proving harder than altering / adding to the existing. Here is the JS function to process the form.

------------ CODE ---------
save: function( groupid ){
var name= jQuery('#community-group-name').val();
var description= jQuery('#community-group-description').val();
var website= jQuery('#community-group-website').val();

var testing_1 = jQuery("input[name$='geektag1']:checked").val();
var testing_2 = jQuery("input[name$='geektag2']:checked").val();
var testing_3 = jQuery("input[name$='geektag3']:checked").val();
var testing_4 = jQuery("input[name$='geektag4']:checked").val();
var testing_5 = jQuery("input[name$='geektag5']:checked").val();
var testing = testing_1 + testing_2 + testing_3 + testing_4 + testing_5;

var category= testing;
var approvals= jQuery("input[@name='group-approvals']:checked").val();

jax.call('community' , 'groups,ajaxSaveGroup' , groupid , name , description , website , category , approvals);
},
------------ CODE ---------

As you can see, I have added the variables "testing" to keep it simple for now. This does work, though it produces a result that I am not happy with, and it becomes bulky as I have around 80 - 100 'geektags' to introduce. Also, here is a sample output based on selecting checkbox 1, 3, and 5. "1¦undefined3¦undefined5¦"

Here is what I want it to do. I want the function that reads and processes the form to have a loop like my checkboxes have, so I do not have to alter the file should I add more than five 'geektags'. Also, I want to be rid of the 'undefined' areas. In the example above, I want the output to be 1¦3¦5

Please, can anyone help me? I have had no professional learning on these subjects, yet I have been able to teach myself PHP mySQL and so forth... Javascripting and JQuery is seeming to be beyond me.

Bazil

PS: for some reason the BBS code tags were not working properly, so I added ------------ CODE --------- to signify. My apologizes.

daveVk

4:00 am on Aug 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try this (There is probably a better way with jQuery functions)

<div class="clabel" id="clabel">

var divEl = document.getElementById( "cLabel" );
var iEls = divEl.getElementsByTagName( "input" ); //all inputs within clabel
var nxEl;
var aS = "";
for ( var i=0; (nxEl=iEls[i++]; ) {
if (nxEl.checked===true) { aS += nxEl.value + '¦' )
}

bazil greyson

4:27 am on Aug 18, 2009 (gmt 0)

10+ Year Member



Thanks for the reply, however just as you were posting it I was smashing my head into that rock one last time and came up with this...

var category = "";
var num = 1;
while (num<=5)
{
var tag = "geektag" + num;
var checkbox = jQuery("input[name$='" + tag + "']:checked").val();
if (checkbox==undefined) { var checkbox = ""; }
var category = category + checkbox;
num++;
}

And it works like a charm!

bazil greyson

4:29 am on Aug 18, 2009 (gmt 0)

10+ Year Member



I'll later add an element to detect how many checkboxes get made by the PHP, but that part is easy now

daveVk

6:04 am on Aug 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wonder why some seem to have no value ( undefined ) ?

bazil greyson

10:52 am on Aug 18, 2009 (gmt 0)

10+ Year Member



If the user does not check the checkbox, it comes back as no value.