Egglabs
Egglabs

Reputation: 3186

How to submit checkbox values with PHP post method

<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

Upvotes: 10

Views: 89883

Answers (7)

Shashank Mishra
Shashank Mishra

Reputation: 1

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;
?>

Upvotes: 0

jeevesh kumar
jeevesh kumar

Reputation: 69

$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 .' ';

Upvotes: 0

user2013
user2013

Reputation: 538

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

Upvotes: 12

Sashi
Sashi

Reputation: 686

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')") ...

Upvotes: 0

Rima
Rima

Reputation: 41

<?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 .' ';
?>

Upvotes: 4

vsr
vsr

Reputation: 3198

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 . ' ';
}

Upvotes: 26

cletus
cletus

Reputation: 625465

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);

Upvotes: 5

Related Questions