Jump to content

Get HTML check box values and then echo values


xerox02

Recommended Posts

<html>

<body>

<form method="post" action="path to script">

<input type="checkbox" id="colors[]" value="red" /> Red

<input type="checkbox" id="colors[]" value="blue" /> Blue

<input type="checkbox" id="colors[]" value="green" /> Green

<input type="checkbox" id="colors[]" value="yellow" /> Yellow

</form>

</form>

</body>

</html>

 

<?php

 

$colors=($_POST['colors']);

echo "What color was chosen? =" . $colors;

 

?>

 

I am trying make a variable that gets the values of check boxes, and then echoes them.

<html>

<body>

<form method="post" action="">

<input type="checkbox" name="colors[]" value="red" /> Red

<input type="checkbox" name="colors[]" value="blue" /> Blue

<input type="checkbox" name="colors[]" value="green" /> Green

<input type="checkbox" name="colors[]" value="yellow" /> Yellow

<input type="submit" value="Submit" name="col_sub">

</form>

</body>

</html>

 

<?php

 

if(isset($_POST['col_sub'])){

      $colors=$_POST['colors'];

      echo "What color was chosen? =";

      for($i=0;$i<count($colors);$i++){

            echo $colors[$i]." ";

      }

}

 

?>

Thanks a lot man :)

If there a way I can get values without having a submit box?

Also, is there a way where I could just have button I designed, and if I click the button it would be as if it were checked. After that I can echo the checked values into a variable

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.