关闭
dsjbest2014 2015-05-28 01:29
浏览 45
已采纳

表单提交复选框值

I have a bike
I have a car

I know when the form is submitted the value will be vehicle=Bike&vehicle=Car if both are ticked

Is there a way to make the value to be vehicle=Bike,Car

Put them into one variable then separated in a comma

  • 写回答

2条回答 默认 最新

  • dongyu3712 2015-05-28 01:40
    关注

    Since you are using POST and multiple check boxes, set the name of each check box like this:

    <input type="checkbox" name="vehicle[]" value="Bike">
    <input type="checkbox" name="vehicle[]" value="Car">
    

    Then when your form is submitted you will receive an array of all the checked boxes and their values in the array:

    $_POST['vehicle'][];
    

    Now if both boxes are checked you can retrieve the values in a foreach loop:

    foreach($_POST['vehicle'] as $type){
       echo "Type = ".$type;
    }
    

    With this you will get an output of

    Type = Bike
    Type = Car
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部