Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.