Welcome Guest, Not a member yet? Register   Sign In
Check for checked checkboxes
#1

[eluser]murphy2006[/eluser]
Hello Guys!

I am desperately trying to loop though a set of checkboxes to see which ones that are check (information from the database).

It works fine but for each checked item I get one unchecked of the same kind. For example if I check the box Singel, I get two checkboxes called Singel (one check and one unchecked) when reloading the page. Why is that?

Thankful for all help!

Code:
<?php

$words = array(

'Singel'          =>  1,
'Separerad'       =>  2,
'Skild'           =>  3,
'Änka/Änkeman'    =>  4

);

?>

<?php $comma = explode(",", $details->par_marital); ?>

<?php foreach ($words as $key => $val) { ?>

<?php foreach ($comma as $line) { ?>

<?php if($val == $line) { ?>

<div class="checkbox_field">&lt;input type="checkbox" class="checkbox" name="profile_marital[]" id="profile_marital[]" value="&lt;?php echo $val; ?&gt;" checked="checked" /&gt;&lt;label>&lt;?php echo $key ?&gt;</label></div>

&lt;?php } else { ?&gt;

<div class="checkbox_field">&lt;input type="checkbox" class="checkbox" name="profile_marital[]" id="profile_marital[]" value="&lt;?php echo $val; ?&gt;" /&gt;&lt;label>&lt;?php echo $key ?&gt;</label></div>

&lt;?php } ?&gt;

&lt;?php } ?&gt;

&lt;?php } ?&gt;
#2

[eluser]Johan André[/eluser]
Ahhh a swedish guy! Smile
#3

[eluser]Nexus Rex[/eluser]
There is a much easier way to preset values from a database...

I/we have solved this problem very easily with a custom helper and new functions called:
preset_select()
preset_checkbox()
preset_radio()

Get all the code and an example in the post here:
http://ellislab.com/forums/viewthread/113120/#691338




Theme © iAndrew 2016 - Forum software by © MyBB