Michaël
Michaël

Reputation: 3671

Retrieving values from a checkbox

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

Upvotes: 2

Views: 2522

Answers (4)

Akash Gupta
Akash Gupta

Reputation: 318

You may use some thing like this..

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

Upvotes: 1

Suman Banerjee
Suman Banerjee

Reputation: 1961

Print all checkbox value.

<form action="print.php" method="post">

 <input type="checkbox" name="val_list[]" value="value 1">
 <input type="checkbox" name="val_list[]" value="value 2">
 <input type="checkbox" name="val_list[]" value="value 3">
 <input type="checkbox" name="val_list[]" value="value 4">
 <input type="checkbox" name="val_list[]" value="value 5">
 <input type="submit" />

</form>

<?php
      if(!empty($_POST['val_list']))
         {
        foreach($_POST['val_list'] as $data)
             {
                echo $data; 
             }
         }
   ?>

Upvotes: 0

anddoutoi
anddoutoi

Reputation: 10111

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

Upvotes: 8

user47322
user47322

Reputation:

Change name to cours[checkbox-value].

This will make an associative array full over selected checkboxes.

Upvotes: 1

Related Questions