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

6
  • your $_POST['travel'] is already an array Commented Jun 4, 2015 at 7:23
  • No need of foreach because your $_POST['travel'] is itself an array with that 3 selected value use that directly.thanks. Commented Jun 4, 2015 at 7:24
  • There should be a space between checkbox"name. $html='You selected '; foreach($_POST['travel']as $selected) $html.=$selected." "; Commented Jun 4, 2015 at 7:28
  • I am not sure you excpeted this is var select = implode(",",$_POST['travel']); now you get the value using {{select}} in mail template its returns bus,train Commented Jun 4, 2015 at 7:31
  • Thank you sathish. what is the difference between var selcet and $select?can I use $select = implode(",",$_POST['travel']); Commented Jun 4, 2015 at 7:57

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

it is correct answer. why down-voted.? people are insane. i up-voted to you.
Even I don't know whats the reason behind the downvoting. Anyways thanks @anantkumarsingh
0

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

Change it it $select[]=$selected;

Comments

0

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"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.