Jump to content

Checkboxes


ecopetition

Recommended Posts

I have the following HTML inside a form:

<input type="checkbox" name="multiple_choice[0]"/>
<input type="checkbox" name="multiple_choice[1]"/>
<input type="checkbox" name="multiple_choice[2]"/>
<input type="checkbox" name="multiple_choice[3]"/>
<input type="checkbox" name="multiple_choice[4]"/>

 

On submitting the form, I need it so that it the PHP can tell whether the checkbox is on or off for each row.

 

How can I do this?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/133900-checkboxes/
Share on other sites

Example:

<?php

if(isset($_POST['submit']))
{
    echo '<p><b>You chose:</b> ' . implode(', ', $_POST['multiple_choice']) . '</p>';
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
$choices = array('Chocolate', 'Milk', 'Candy', 'Apple', 'Coca Cola');
echo 'What do you like?<br />';
foreach($choices as $key => $choice)
{
    $chkd = (isset($_POST['multiple_choice'][$key])) ? 'checked="checked" ' : null;
    echo '<input type="checkbox" name="multiple_choice['.$key.']" value="'.$choice.'" '.$chkd.'/> '.$choice."<br />\n";
}
?>
<input type="submit" name="submit" value="Submit" />
</form>

Link to comment
https://forums.phpfreaks.com/topic/133900-checkboxes/#findComment-697015
Share on other sites

Is there a script that can just return on or off for each entry, regardless of how many checkboxes there is?

 

For example,

 

[0] => on,

[1] => on,

[2] => off

Yea, how I just showed you. There is no other way... other than:

<input type="checkbox" name="multiple_choice[0]" <?php if(isset($_POST['multiple_choice'][0])) echo 'checked="checked" '; ?>/>
<input type="checkbox" name="multiple_choice[1]" <?php if(isset($_POST['multiple_choice'][1])) echo 'checked="checked" '; ?>/>
<input type="checkbox" name="multiple_choice[2]" <?php if(isset($_POST['multiple_choice'][2])) echo 'checked="checked" '; ?>/>
<input type="checkbox" name="multiple_choice[3]" <?php if(isset($_POST['multiple_choice'][3])) echo 'checked="checked" '; ?>/>
<input type="checkbox" name="multiple_choice[4]" <?php if(isset($_POST['multiple_choice'][4])) echo 'checked="checked" '; ?>/>

But that is just very repetitive, Which is why I opted to use an array for generating my checkboxes in my example.

<?php
$choices = array('Chocolate', 'Milk', 'Candy', 'Apple', 'Coca Cola');
echo 'What do you like?<br />';
foreach($choices as $key => $choice)
{
    $chkd = (isset($_POST['multiple_choice'][$key])) ? 'checked="checked" ' : null;
    echo '<input type="checkbox" name="multiple_choice['.$key.']" value="'.$choice.'" '.$chkd.'/> '.$choice."<br />\n";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/133900-checkboxes/#findComment-697224
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.