How to submit checkbox values with PHP post method

后端 未结 7 562
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 12:48






        
相关标签:
7条回答
  • 2020-12-09 13:22

    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 . ' ';
    }
    
    0 讨论(0)
  • 2020-12-09 13:27

    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 讨论(0)
  • 2020-12-09 13:33

    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);
    
    0 讨论(0)
  • 2020-12-09 13:39

    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')") ...
    
    0 讨论(0)
  • 2020-12-09 13:40

    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

    0 讨论(0)
  • 2020-12-09 13:44
    $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 .' ';
    
    0 讨论(0)
提交回复
热议问题