Jump to content

get check box value


hi2you

Recommended Posts

Hello,

 

I have a list of check box

 

<form name="form" action="test.php" method="POST">

 

<input type="checkbox" name="option1" value="a"> a<br>

<input type="checkbox" name="option2" value="b" > b<br>

<input type="checkbox" name="option3" value="c"> c<br>

 

</form>

 

and in test.php,  I have a String varible $text, and if a and b are checked, then $text = "ab".

 

if a, c are checked, then $text becomes "ac".

 

How to do so??

 

Thanks!!

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

try this,

 

form page...

<form name="form" action="test.php" method="POST">
<input type="checkbox" name="opt[]" value="a"> a
<input type="checkbox" name="opt[]" value="b" > b
<input type="checkbox" name="opt[]" value="c"> c
<input type="submit" />
</form>

 

test.php...

<?php
foreach($_POST['opt'] as $opt)
$text .= $opt;
echo $text;
?>

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

or

 

<form action="test.php" method="post">
<input type="checkbox" name="option1" value="a" /> a
<input type="checkbox" name="option2" value="b" /> b
<input type="checkbox" name="option3" value="c" /> c
<input type="submit" />
</form>

 

test.php

<?php
$text = '';
for ($i = 1; $i <= 3; $i++) {
	$text .= $_POST['option'.$i];
}
?>

 

To answer your question, unchecked checkboxes won't get submitted at all.

Link to comment
https://forums.phpfreaks.com/topic/93641-get-check-box-value/#findComment-479795
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.