Question : Joomla Checkbox Array (need expert!)

If I could award 5000 points for this I would…. Hopefully there is a Joomla/PHP master out there who can help with this.

Inside an admin page I have placed a number of checkboxes in a logical group.  They are created based on the results of a database query.   So the number of checkboxes will vary.

So far I have successfully created and 'checked' the appropriate boxes automatically in my code.  You can see my existing and successfully functioning code in the attachment.

These checkboxes are inside a form that uses the post method.  The form operates and submits properly.

My challenge is in locating the checkbox values in the post array after the form is submitted.  It appears that they don't exist.  Perhaps my syntax is wrong when I create the check boxes or I am not using the correct syntax in testing their respective state after the form is submitted.

I can't seem to pickup this checkbox array.  Is the problem in the syntax in the checkbox declaration (see attachment starts around line 20)  or in how I am trying to address the $_POST array in the subsequent page (see below)?

If I use Jrequest::get('assignedTeams',''.'post','array', JREQUEST_ALLOWRAW) in the subsequent page that is called by submitting the form, I get 'Notice: Undefined index: assignedTeams' when I try to print_r or echo that array.

Could someone have a look at my code and help me understand how to test the values of a checkbox array after a form has been submitted in Joomla?  I suspect that I am not using the correct syntax or don't understand how joomla handles  $_POST

By the way the checkboxes work and render fine etc.

Thanks!

HNM
1:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
<form action="index.php?option=<?php echo $option?>" method="post" name="adminForm" enctype="multipart/form-data">

<table>
<tr>

		<td width="100">

			<?php echo JText::_( 'Team' ); ?>

<span class="editlinktip hasTip" title="<?php echo JText::_( 'Team(s)' ); ?>::<?php echo JText::_( 'Select Player team(s)' );?>"><img src="components/com_bearleague/img/quest.jpg" border="0" /></span>

		</td>

		<td>
			<table style="border:thin #cdcdcd solid;margin:1em 0 1em 0;">						
					
					<?php 
			
					
					//test for and current team associations (if the players is in the roster table)
					
					if (!count($lists['currentTeams'])) {
						//if the player is not on a team already then just display check boxes for all potential teams
						for($i=0;$i<count($lists['teams']);$i++) {
							
							if ($i<=2) {
								echo "<tr><td><input type='checkbox' name='assignedTeams[".$i."]' value='".$lists['teams'][$i]->id."'/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							}
							
							if ($i>2 and $i<=5) {
															 
								echo "<tr><td><input type='checkbox' name='assignedTeams[".($i+1)."]' value='".$lists['teams'][$i]->id."'/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							
							}
							
							if ($i>5 and $i<=8) {
							 
								echo "<tr><td><input type='checkbox' name='assignedTeams[".($i+2)."]' value='".$lists['teams'][$i]->id."'/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
								
							}
							
							if ($i>8 and $i<=11) {
							 
								echo "<tr><td><input type='checkbox' name='assignedTeams[".($i+3)."]' value='".$lists['teams'][$i]->id."'/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							}

						}
					
					}
					
					
					else {
					
					
						//show all of the team checkboxes and if the player is on that team, set value to checked
						for($i=0;$i<count($lists['teams']);$i++) {
							
							if ($i<=2) {

							  	//check to see if the player is a current member of this team, if so, place a check in the checkbox
							  		for($j=0;$j<count($lists['currentTeams']);$j++) {
							  			if ($lists['currentTeams'][$j]->team_id == $lists['teams'][$i]->id) {
							  				echo "<tr><td><input type='checkbox' name='assignedTeams[".$i."]' value='".$lists['teams'][$i]->id."' checked/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							  			}
							  			else {
							  				echo "<tr><td><input type='checkbox' name='assignedTeams[".$i."]' value='".$lists['teams'][$i]->id."'/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							  			}
							  		}
							  		
							}
							
							if ($i>2 and $i<=5) {
															 
									//check to see if the player is a current member of this team, if so, place a check in the checkbox
							  		for($j=0;$j<count($lists['currentTeams']);$j++) {
							  			if ($lists['currentTeams'][$j]->team_id == $lists['teams'][$i]->id) {
							  				echo "<tr><td><input type='checkbox' name='assignedTeams[".($i+1)."]' value='".$lists['teams'][$i]->id."' checked/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							  			}
							  			else {
							  				echo "<tr><td><input type='checkbox' name='assignedTeams[".($i+1)."]' value='".$lists['teams'][$i]->id."'/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							  			}
							  		}
							}
							
							if ($i>5 and $i<=8) {
							 
									//check to see if the player is a current member of this team, if so, place a check in the checkbox
							  		for($j=0;$j<count($lists['currentTeams']);$j++) {
							  			if ($lists['currentTeams'][$j]->team_id == $lists['teams'][$i]->id) {
							  				echo "<tr><td><input type='checkbox' name='assignedTeams[".($i+2)."]' value='".$lists['teams'][$i]->id."' checked/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							  			}
							  			else {
							  				echo "<tr><td><input type='checkbox' name='assignedTeams[".($i+2)."]' value='".$lists['teams'][$i]->id."'/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							  			}
							  		}
							}
							
							if ($i>8 and $i<=11) {
							 
									//check to see if the player is a current member of this team, if so, place a check in the checkbox
							  		for($j=0;$j<count($lists['currentTeams']);$j++) {
							  			if ($lists['currentTeams'][$j]->team_id == $lists['teams'][$i]->id) {
							  				echo "<tr><td><input type='checkbox' name='assignedTeams[".($i+3)."]' value='".$lists['teams'][$i]->id."' checked/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							  			}
							  			else {
							  				echo "<tr><td><input type='checkbox' name='assignedTeams[".($i+3)."]' value='".$lists['teams'][$i]->id."'/></td><td>".$lists['teams'][$i]->t_name."</td></tr>";
							  			}
							  		}
							}

						}
					}
					
						
					?>
						
			</table>
					
<input type="hidden" name="option" value="<?php echo $option?>" />

<input type="hidden" name="task" value="" />

<input type="hidden" name="id" value="<?php echo $row->id?>" />

<input type="hidden" name="boxchecked" value="0" />

<?php echo JHTML::_( 'form.token' ); ?>

</form>

Answer : Joomla Checkbox Array (need expert!)

In your form, you are naming the check boxes like so:

assignedTeams[1]
assignedTeams[2]
assignedTeams[3]

If at least one is checked, then your POST array will contain a key called assignedTeams, and the values of this key will be stored in an array. The indexes that are available in the assignedTeams array will depend on which checkboxes have been ticked.

For example, if only assignedTeams[2] is ticked, then you can access it with this:

$_POST['assignedTeams'][2]

No other indexes will exist !

You can loop through the selected assignedTeams checkboxes with this code:

foreach ($_POST['assignedTeams'] as $index => $value) {
    echo "<p>The checkbox with an index of $index has a value of $value</p>"
}

Remember, assignedTeams will only exist in your POST array if at least one value is ticked, and it will only contain the details of the checkboxes that were ticked.




Random Solutions  
 
programming4us programming4us