开发者

How to submit checkbox values with PHP post method

开发者 https://www.devze.com 2022-12-21 01:20 出处:网络
<input type=\"checkbox\" class=\'form\' name=\"checkbox_1\" /> <input type=\"checkbox\" class=\'form\' name=\"checkbox_2\" />
<input type="checkbox" class='form' name="checkbox_1" />

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

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

...开发者_JAVA百科......

<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


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


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


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


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


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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消