Jump to content

get check box value


phppaper

Recommended Posts

How can I use php to get the check boxs values (only the checked one):

 

if you are posting it using a PHP form something in this manner

($_POST['checkboxname] == "ON")

 

or a group of checkboxes

 

page_html.php

 

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

      <input type="checkbox" id="colour[]" value="red"> Cat<br>

      <input type="checkbox" id="colour[]" value="blue"> Mouse<br>

      <input type="checkbox" id="colour[]" value="green"> Car<br>

      <input type="checkbox" id="colour[]" value="yellow"> DaniWeb

      </form>

 

php.php

 

      <?php

      $things = serialize($_POST['colour']);

 

      $query = "INSERT INTO table(colour) values($colour)";

      mysql_query($query) or die(mysql_error());

      ?>

 

Just turn it into an array and dump the contents to db. You have to "serialize" the data first and don't forget to "unserialize" when you are taking the data back out.

Link to comment
https://forums.phpfreaks.com/topic/206856-get-check-box-value/#findComment-1081775
Share on other sites

Dear all,

 

How can I use php to get the check boxs values (only the checked one):

 

<input type="checkbox" name="option[]" value="Milk"> Milk<br>

<input type="checkbox" name="option[]" value="Butter"> Butter<br>

<input type="checkbox" name="option[]" value="Cheese"> Cheese

 

Thanks!!

 

Hi,

 

if (isset($_POST['option']) && is_array($_POST['option']))
{
foreach ($_POST['option'] as $option)
{
	echo $option.', ';
}
}

Link to comment
https://forums.phpfreaks.com/topic/206856-get-check-box-value/#findComment-1081788
Share on other sites

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.