Comment faire pour afficher des cases à cocher à partir d'un formulaire HTML par PHP?

J'ai un formulaire HTML comme suit:

<form action="something.php" method="POST" enctype="multipart/form-data" id="color"> <fieldset> <legend><h2><span style="color: #993300; text-shadow: #808080 2px 1px 2px; font-size: 25px;">Color Preferences:</span></h2></legend>&nbsp; <p><table> <tbody> <tr> <td width="350px;"><span style="color: #993300;">*</span> Colors you consider to use to your new Site:</td> <td> <br/> <input type="checkbox" name="color[]" value="Black" /> Black<br/> <input type="checkbox" name="color[]" value="Blue" /> Blue<br/> <input type="checkbox" name="color[]" value="Brown" /> Brown<br/> <input type="checkbox" name="color[]" value="Gray" /> Gray<br/> <input type="checkbox" name="color[]" value="Green" /> Green<br/> </td> </tr> </tbody> </table></p> </fieldset> <input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /> 

Pour exécuter la forme de couleur, j'ai utilisé un PHP comme:

 $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } $ color = $ _POST ['color']; $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } si (! isset ($ color)) $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } { $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } désactivé ($ _ GET ['do']); $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } $ message = "Erreur = Les colors sont requirejses. Veuillez réessayer."; $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } $ N = count ($ color); $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } pour ($ i = 0; $ i <$ N; $ i ++) { $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } echo ($ color [$ i]. ""); $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } } $color = $_POST['color']; if (!isset($color)) { unset($_GET['do']); $message = "Error = Colors are required. Please try again."; break; } else { $N = count($color); for($i=0; $i < $N; $i++){ echo($color[$i] . " "); } } 

Mais lorsque je teste mon code sur la zone de sortie, il ne donne qu'un "Array" en conséquence. Pouvez-vous m'aider à corriger ce code? (Je dois get les colors sélectionnées comme une sortie au lieu de "Array".)

J'ai même changé mes codes en:

 $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } $ color = $ _POST ['color'] && $ _POST ['couleur'] = tableau ('Noir', 'Bleu', 'Marron', 'Gris', 'Vert', 'Orange', 'Rose', ' Purple ',' Red ',' Silver ',' Tan ',' White ',' Yellow ',' Dark ',' Light '); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } si (! isset ($ _ POST ['couleur'])) $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } { $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } désactivé ($ _ GET ['do']); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } echo "Erreur = Les colors sont requirejses. Veuillez réessayer."; $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } $ n = count ($ _ POST ['color']); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } echo ("Vous avez sélectionné $ n color (s):"); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } pour ($ i = 0; $ i <$ n; $ i ++) $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } { $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } echo ($ _ POST ['couleur'] [$ i]. ""); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } } $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } 

mais le résultat final n'est que "Array" au lieu du nom des colors. pouvez-vous m'aider pour résoudre ce problème?


J'ai même changé mes codes en:

 $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } $ color = $ _POST ['color'] && $ _POST ['couleur'] = tableau ('Noir', 'Bleu', 'Marron', 'Gris', 'Vert', 'Orange', 'Rose', ' Purple ',' Red ',' Silver ',' Tan ',' White ',' Yellow ',' Dark ',' Light '); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } si (! isset ($ _ POST ['couleur'])) $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } { $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } désactivé ($ _ GET ['do']); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } echo "Erreur = Les colors sont requirejses. Veuillez réessayer."; $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } $ n = count ($ _ POST ['color']); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } echo ("Vous avez sélectionné $ n color (s):"); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } pour ($ i = 0; $ i <$ n; $ i ++) $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } { $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } echo ($ _ POST ['couleur'] [$ i]. ""); $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } } $color = $_POST['color'] && $_POST['color'] = array ('Black','Blue','Brown','Gray','Green','Orange','Pink','Purple','Red','Silver','Tan','White','Yellow','Dark','Light'); if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } } 

mais le résultat final n'est que "Array" au lieu du nom des colors. pouvez-vous m'aider pour résoudre ce problème?

Essaye ça:

Code PHP:

if (isset ($ _ POST ['submit'])) {

  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } if (! isset ($ _ POST ['color'])) {  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } désactivé ($ _ GET ['do']);  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } echo "Erreur = Les colors sont requirejses. Veuillez réessayer.";  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } $ color = $ _POST ['color'];  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } $ N = count ($ color);  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } pour ($ i = 0; $ i <$ N; $ i ++) {  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } echo ($ color [$ i]. "");  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } }  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } }  if (!isset($_POST['color'])) { unset($_GET['do']); echo "Error = Colors are required. Please try again."; } else { $color = $_POST['color']; $N = count($color); for ($i = 0; $i < $N; $i++) { echo($color[$i] . " "); } } } 

avec la couleur [], vous recevrez un tableau ($ _POST ['couleur']). Vous pouvez tester si c'est isset () ou il est vide ()

si ce n'est pas vide, vous pouvez ensuite itérer sur les valeurs par exemple comme ceci:

 $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } $ n = count ($ _ POST ['color']); $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } echo ("Vous avez sélectionné $ n color (s):"); $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } pour ($ i = 0; $ i <$ n; $ i ++) $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } { $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } echo ($ _ POST ['couleur'] [$ i]. ""); $n = count($_POST['color']); echo("You selected $n color(s): "); for($i=0; $i < $n; $i++) { echo($_POST['color'][$i] . " "); } 

(non testé. Code pris à partir d'ici: http://www.html-form-guide.com/php-form/php-form-checkbox.html )