简体   繁体   中英

How to store selected multiple checkbox values in an array in php?

<input type="checkbox"name="travel[]" value="bus"/>
<input type="checkbox"name="travel[]" value="train"/>
<input type="checkbox"name="travel[]" value="plane"/>
foreach($_POST['travel']as $selected)
  var select[]=$selected;

If the user selects all the three checkboxes, I have to store them in an array and send it to mail as I dont have a data base. So how should I store them in an array?

foreach($_POST['travel']as $selected)
var select[]=$selected;

The above code is returning only last selected check box And how should I pass it and display it on the mail?

Instead of

foreach($_POST['travel']as $selected)
var select[]=$selected;

update it to

$select = array();
foreach($_POST['travel'] as $key => $selected){
    $select[$key]=$selected;
}

Instead of using foreach simply use $select = implode(',',$_POST['travel']);

Because every time you are defining a new array, by var select[]=$selected; .

Change it it $select[]=$selected;

please give different names as below. after posting data using foreach loop you will get all selected options <input type="checkbox" name="travel1[]" value="bus"/> <input type="checkbox" name="travel2[]" value="train"/> <input type="checkbox" name="travel3[]" value="plane"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM