tags:

views:

521

answers:

3

Hi,
I use a php checkbox and I want to retrieve marked values.
My checkbox code :

<label for="cours">Je suis intéressé par un ou plusieurs cours :</label><br><br>
<input type="checkbox" name="cours" value="individuel">Individuel<br>
<input type="checkbox" name="cours" value="semiprive">Semi-privé<br>
<input type="checkbox" name="cours" value="minigroupe">Mini-groupe<br>
<input type="checkbox" name="cours" value="intensif">Intensif<br>
<input type="checkbox" name="cours" value="entreprise">Entreprises<br>
<input type="checkbox" name="cours" value="distance">A distance<br>
<input type="checkbox" name="cours" value="telephone">Par téléphone<br>
<input type="checkbox" name="cours" value="coaching">Coaching<br>
<input type="checkbox" name="cours" value="soutien">Soutien scolaire<br>
<input type="checkbox" name="cours" value="diplome">Diplômes officiels<br>

php :

<?php
  if(isset($_POST['envoyer']))
  {
    if(get_magic_quotes_gpc())
    {
      $cours = stripslashes(trim($_POST['cours']));
    }
  }
?>

I want to put it in the variable msg :

$msg = 'Cours : '.$cours."\r\n";

and sending the message throw the php email function.
But when I do that like this a receive just the first checked choice...
Thank you for your help.
Michaël

+9  A: 

You have to change the name attribute to cours[] and then php will treat it as an array.

Read up at http://docs.php.net/faq.html

anddoutoi
But know I obtain this : "Cours : Array" when I receive the email...How can I do to see the values contain in the array?
Michaël
I will read the doc. Thanks.
Michaël
+1 for referencing the documentation. -1 for referencing the italian documentation.
Gumbo
try a <pre><?php print_r($_POST['cours']); ?></pre> and you´ll see all checked options.
anddoutoi
@Gumbo hehehe, didn´t notice the 'it' in the URL, just copied the first result in my google search, but I´ll edit it ^^
anddoutoi
@anddoutoi: print_r() prints shit, it doesn't return a nice string
Charlie Somerville
Do I have to write : <label for="cours"> or <label for="cours[]">?
Michaël
@Michaël: Nae, the label[for] attribute is connected to the input[id] attribute, not the input[name].
anddoutoi
@Charlie: well, you have to treat it as an array ofc. If you want a comma separated string i suggest you use implode/join (http://docs.php.net/implode).
anddoutoi
@anddoutoi: Thank you, I use : $coursListe = implode(', ',$cours); and it returns the list of the elements of the array.
Michaël
@Michaël: Your most welcome, glad I could be of help =)
anddoutoi
To be sure that the array is not empty I have done that : $count = count($cours);if($count!=0){$coursListe = implode(', ',$cours);}else{$coursListe = 'aucun';}otherwise I obtain a warning.
Michaël
A: 

because all check box have same name

Haim Evgi
+1  A: 

Change name to cours[checkbox-value].

This will make an associative array full over selected checkboxes.

Charlie Somerville