Forums

Checkboxes

lchessar 05 Dec, 2007
I have created a form where the user has 6 options to each question and is required to 'tick all that apply'. I've used checkboxes (see code below) but Chronoforms is only storing the value of the last checked box, not all that have been ticked.

<tr align="left">
<td width="53" align="center"><input type="checkbox" name="one" value="one"></td>
<td width="497">answer 1 </td>
</tr>
<tr align="left">
<td align="center"><input type="checkbox" name="one" value="two"></td>
<td>answer 2</td>
</tr>
<tr align="left">
<td align="center"><input type="checkbox" name="one" value="three"></td>
<td>answer 3 </td>
</tr>
<tr align="left">
<td align="center"><input type="checkbox" name="one" value="four"></td>
<td>answer 4 </td>
</tr>
<tr align="left">
<td align="center"><input type="checkbox" name="one" value="five"></td>
<td>answer 5 </td>
</tr>
<tr align="left">
<td align="center"><input type="checkbox" name="one" value="six"></td>
<td>answer 6 </td>
</tr>

Can you please let me know where I am going wrong?

Thanks

Laura
GreyHead 05 Dec, 2007
Hi Laura,

You need to have the checkboxes return an array of results. Use name="one[]"

Bob
lchessar 05 Dec, 2007
Thanks - I've tried that one already but the answer I get is 'Array' not 'one, two, three, four etc.' as expected.
GreyHead 05 Dec, 2007
Hi Laura,

OK, in that case keep the [] and edit the AutoGenerated code to replace $_POST['one'] with implode(', ', $_POST['one']) - this should convert the array to a comma seperated list.

I think that should work OK in the MySQL, if it doesn't then we'll need to put something into the OnSubmit code field.

Bob
lchessar 05 Dec, 2007
Worked a treat! Thanks Bob.
lchessar 08 Jan, 2010
Hi Greyhead,

I'm trying to implode an array on a checkbox called whynot[].

I'm using this code in the autogenerated code:

implode(', ', $_POST['whynot'])
to replace
$_POST['whynot']

But it only gives me the first character of the first response. How can I fix it?

Thanks in advance,

Laura. (chronoforms 2.3.9)
GreyHead 09 Jan, 2010
Hi Laura,

I no longer remember clearly how the code works in Joomla 1.0 :-(

The first character seems odd. What is the column format? I wonder if that isn't long enough?

Can you post the full code autogenerated code block please?

Bob
lchessar 11 Jan, 2010
Appreciate your help Greyhead - thanks. Here is the original code. I've since amended the field names in my form to whynot[] and sateast[] , the two checkbox fields.


<?php 
	global $database;
	
	srand((double)microtime()*10000);
	$inum	=	"I" . substr(base64_encode(md5(rand())), 0, 16);
	
	
$database->setQuery( "INSERT INTO #__chronoforms_6 VALUES  (
'' , '".$inum."','". date('Y-m-d')." - ".date("H:i:s")."', '".$_SERVER['REMOTE_ADDR']."' , '".mosGetParam($_POST,'current','')."' , '".mosGetParam($_POST,'decision1','')."' , '".mosGetParam($_POST,'decision2','')."' , '".mosGetParam($_POST,'decision3','')."' , '".mosGetParam($_POST,'decision4','')."' , '".mosGetParam($_POST,'decision5','')."' , '".mosGetParam($_POST,'decision6','')."' , '".mosGetParam($_POST,'decision7','')."' , '".mosGetParam($_POST,'decision8','')."' , '".mosGetParam($_POST,'likely1','')."' , '".mosGetParam($_POST,'likely2','')."' , '".mosGetParam($_POST,'likely3','')."' , '".mosGetParam($_POST,'likely4','')."' , '".mosGetParam($_POST,'long','')."' , '".mosGetParam($_POST,'manyhome','')."' , '".mosGetParam($_POST,'manyaway','')."' , '".mosGetParam
($_POST,'whynot','')."' , '".mosGetParam
($_POST,'whynotother','')."' , '".mosGetParam($_POST,'stand','')."' , '".mosGetParam($_POST,'wfactors1','')."' , '".mosGetParam($_POST,'wfactors2','')."' , '".mosGetParam($_POST,'wfactors3','')."' , '".mosGetParam($_POST,'wfactors4','')."' , '".mosGetParam($_POST,'wfactors5','')."' , '".mosGetParam($_POST,'wfactors6','')."' , '".mosGetParam($_POST,'wfactors7','')."' , '".mosGetParam($_POST,'wfactors8','')."' , '".mosGetParam($_POST,'wfactors9','')."' , '".mosGetParam($_POST,'wfactors10','')."' , '".mosGetParam($_POST,'efactors1','')."' , '".mosGetParam($_POST,'efactors2','')."' , '".mosGetParam($_POST,'efactors3','')."' , '".mosGetParam($_POST,'efactors4','')."' , '".mosGetParam($_POST,'efactors5','')."' , '".mosGetParam($_POST,'efactors6','')."' , '".mosGetParam($_POST,'efactors7','')."' , '".mosGetParam($_POST,'efactors8','')."' , '".mosGetParam($_POST,'efactors9','')."' , '".mosGetParam($_POST,'efactors10','')."' , '".mosGetParam
($_POST,'sateast','')."' , '".mosGetParam($_POST,'opinion','')."' , '".mosGetParam($_POST,'attend','')."' , '".mosGetParam($_POST,'base1','')."' , '".mosGetParam($_POST,'base2','')."' , '".mosGetParam($_POST,'base3','')."' , '".mosGetParam($_POST,'base4','')."' , '".mosGetParam($_POST,'base5','')."' , '".mosGetParam($_POST,'base6','')."' , '".mosGetParam($_POST,'base7','')."' , '".mosGetParam($_POST,'base8','')."' , '".mosGetParam($_POST,'stadium','')."' , '".mosGetParam($_POST,'kickoff','')."' , '".mosGetParam($_POST,'likelyb1','')."' , '".mosGetParam($_POST,'likelyb2','')."' , '".mosGetParam($_POST,'likelyb3','')."' , '".mosGetParam($_POST,'likelyb4','')."' , '".mosGetParam($_POST,'likelyb5','')."' , '".mosGetParam($_POST,'likelyb6','')."' , '".mosGetParam($_POST,'age','')."' , '".mosGetParam($_POST,'gender','')."' , '".mosGetParam($_POST,'postcode','')."' , '".mosGetParam($_POST,'club','')."' , '".mosGetParam($_POST,'income','')."' , '".mosGetParam($_POST,'ethnic','')."');" );
if (!$database->query()) {
echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>
";
}
?>
GreyHead 11 Jan, 2010
Hi Laura,

I think that you need to implode the array values into a string
$whynot = implode(',', mosGetParam($_POST,'whynot',''));

Bob
lchessar 11 Jan, 2010
Magic1 Works a treat. Thanks very much for your support.
This topic is locked and no more replies can be posted.