10
<input type="checkbox" class='form' name="checkbox_1" />

<input type="checkbox" class='form' name="checkbox_2" />

<input type="checkbox" class='form' name="checkbox_3" />

.........

<input type="checkbox" class='form' name="checkbox_10" />

The from has been submitted using the "POST" method. identify, which of the check-boxes and write their numbers in increasing order. Separated all numbers by spaces(not new lines) and do not use any HTML formatting.

For Eg:

If check-boxes 3, 5 and 10 are checked.

Ouput would be:

3 5 10

3
  • So, what's the problem here ? Commented Feb 22, 2010 at 9:07
  • So what is the issue? Detail out what you wish to have. Commented Feb 22, 2010 at 9:08
  • Is there any reason you did not accept the answer here? Commented Mar 15, 2012 at 12:41

7 Answers 7

26

Change the markup to something like

<input type="checkbox" class='form' value="1" name="checkbox[]" />
<input type="checkbox" class='form' value="2"  name="checkbox[]" />
<input type="checkbox" class='form' value="3"  name="checkbox[]" />

and to get the values submitted, use a simple loop

foreach($_POST['checkbox'] as $checkbox){
    echo $checkbox . ' ';
}
Sign up to request clarification or add additional context in comments.

2 Comments

if (isset($_POST['checkbox']) && is_array($_POST['checkbox'])) { echo implode(' ', $_POST['checkbox']); }
How are you using a php variable checkbox[] in simple HTML form?
12

HTML Code ::

  <input type="checkbox" name="arrayValue[]"  value="value1" > value1 <br />
  <input type="checkbox" name="arrayValue[]"  value="value2" > value2 <br />
  <input type="checkbox" name="arrayValue[]"  value="value3" > value3 <br />
  <input type="checkbox" name="arrayValue[]"  value="value4" > value4 <br />

php code::

 $checkBoxValue = join(", ", $_POST['arrayValue']);  // here first (,) is user-define
                                                   // means, you can change it whatever
                                                // you want, even if it can be (space) or others

now you get the whole value of 'checkbox' in one variable

1 Comment

Help me a great deal! I was trying to send the checkbox array in a email $message on submit. With the accepted answer it would only read the last value and not all of them. This answer worked perfectly, and I think should be the correct answer! Thanks!
5

Iterate over the $_POST array and use preg_match() to pull out the number if it starts with "checkbox_":

$checked = array();
foreach ($_POST as $k => $v) {
  if (preg_match('|^checkbox_(\d+)$!', $k, $matches) {
    $checked[] = $matches[1];
  }
}
echo implode(' ', $matches);

1 Comment

What vsr said seems more appropiate, this way you don't have to search for a field using regexes.
4
<?php
$checked = array();

foreach ($_POST as $k => $v) {
$subject = $k;
$pattern = '/^checkbox_(\d+)$/';
  if (preg_match($pattern, $subject, $matches)) 
  {
    $checked[] = $matches[1];
  }
}

asort($checked);

foreach ($checked as $key=>$value)
echo $value .' ';
?>

Comments

0

For your Each Checkbox value retrieval you can use below method to get values from checkbox are checked or not....

In My Form Page (Monday to Sunday)

 <input type="checkbox" name="checked[]" class="onoffswitch-checkbox" id="mononoffswitch" value="Mon" checked>

In My PHP Code

function IsChecked($chkname,$value)
{
    if(!empty($_POST[$chkname]))
    {
        foreach($_POST[$chkname] as $chkval)
        {
            if($chkval == $value)
            {
                return true;
            }
        }
    }
    return false;
}

if(IsChecked('checked','Mon'))
{
    $checkedMon = "Y";

}else {
    $checkedMon = "N";

}

if(IsChecked('checked','Tue'))
{
    $checkedTue = "Y";

}else {
    $checkedTue = "N";

}

if(IsChecked('checked','Wed'))
{
    $checkedWed = "Y";

}else {
    $checkedWed = "N";

}

if(IsChecked('checked','Thur'))
{
    $checkedThur = "Y";

}else {
    $checkedThur = "N";

}

if(IsChecked('checked','Fri'))
{
    $checkedFri = "Y";

}else {
    $checkedFri = "N";

}

if(IsChecked('checked','Sat'))
{
    $checkedSat = "Y";

}else {
    $checkedSat = "N";

}

if(IsChecked('checked','Sun'))
{
    $checkedSun = "Y";

}else {
    $checkedSun = "N";

}

Now you can get these variable values and can use in to your Insert Into statement ...

Like this

$addweekdays = mysqli_query($conn, "INSERT INTO weekdays(id,monday,tuesday,wednesday,thursday,friday,saturday,sunday) VALUES('$Id', '$checkedMon', '$checkedTue', '$checkedWed', '$checkedThur','$checkedFri','$checkedSat','$checkedSun')") ...

Comments

0
$checked = array();

foreach ($_POST as $k => $v) {
$subject = $k;
$pattern = '/^checkbox_(\d+)$/';
  if (preg_match($pattern, $subject, $matches)) 
  {
    $checked[] = $matches[1];
  }
}

asort($checked);

foreach ($checked as $key=>$value)
echo $value .' ';

Comments

0

take an array for name of your checkbox as name="checkbox[]" and use same name for all your checkbox. After submitting your form receive the values of checked checkbox by using following code:

<?php
$chk="";
foreach($_POST['checkbox'] as $checkbox)
{
$chk=$chk.$checkbox;
}
echo $chk;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.