doushu5805 2017-09-16 11:01
浏览 148
已采纳

如何从php中的复选框中的文本框中获取值

I have 4 checkboxes, one of them is Others have textbox, i want to get all the values that the user checked and if he checked others option to get the values from the textbox associated with Others checkbox.

HTML Code

<div class="row">
    <div class="col-sm-4">
        <label class="Modallabel">Available Products:</label>
    </div>
    <div class="col-sm-8">
        <label id="Pro_chkbox" class="checkbox-inline"><input name="check_list[]" type="checkbox" value="Cacao">Cacao</label>
        <label id="Pro_chkbox" class="checkbox-inline"><input name="check_list[]" type="checkbox" value="Coconuts">Coconuts</label>
        <label id="Pro_chkbox" class="checkbox-inline"><input name="check_list[]" type="checkbox" value="Bananas">Bananas</label><br>
        <label id="Pro_chkbox" class="checkbox-inline"><input name="check_list[]" type="checkbox" id="optcheck" value="Others">Others</label>
        <input type="text" id="Other_pro" name="otherproduct"><br>
        <label id="Note">(Separate Products with commas)</label>
    </div>
</div>

PHP Code

$checked_count = count($_POST['check_list']);

    if ($checked_count > 1)
    { 
        $productlist = implode(', ', $_POST['check_list']);
        echo $productlist;
    }
    elseif ($checked_count == 1)
    { 
        foreach($_POST['check_list'] as $selected) {
            $productlist = $selected;

            //To check if Others checkbox is checked or not to get the values in textbox
            if ($productlist == "Others")
            {
                $productlist = $_POST["otherproduct"];
            }
            echo $productlist;
        }
    }
  • 写回答

1条回答 默认 最新

  • duan032225 2017-09-16 11:54
    关注

    This will do the trick for you

    $checked_count = count($_POST['check_list']);
    $productlist = ''; //initialize an empty string for product list
    if ($checked_count > 1) //check if multiple check-boxes are checked
    { 
        $productlist = implode(', ', $_POST['check_list']); //implode all checkbox values in list string
        if(in_array('Others', $_POST['check_list'])) { //check if others is checked
            $productlist .= ', '.$_POST['otherproduct']; //con-cat text in text box lined with others in list string 
            $productlist = str_replace('Others,', '', $productlist); //remove others from the list string (skip if you want others to be in your result'
        }
    } elseif ($checked_count == 1) {
        $productlist = ($_POST['check_list'][0] == 'Others') ? $_POST['otherproduct'] : $_POST['check_list'][0]; //if only one checkbox is checked then check its value and use the value
    }
    echo "<br/>".$productlist;
    

    Additionally you may put client side and server side validations to be ensure that you get correct input values from your form.

    Also you can use java script to make your form more interactive.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!