PDA

View Full Version : Submit form that has dynamically generated checkboxes



mtran
12-02-2007, 05:27 AM
I'm not well-versed in PHP but managed to dynamically generated a page/list of items, and their descriptions, images.... including a checkbox before each items. That page - page 1 - is ok.



<form name="compareForm" method="post" action="page2.php">
<?php
[loop]
.....
<input type="checkbox" value="<?php print $id ?>" name="compare[]" />
<a href="javascript:document.compareForm.submit();">Compare</a>
[end loop]
?>
</form>


Viewing source shows that the Value of the checkboxes are generated correctly - which are the Item Ids - 1, 2, 3...

However, if I check some checkboxes and submit the form, the values don't seem to be passed to page 2, which has the following codes:



<?php
$compare = $_POST["compare"];
foreach ($compare as $value) {
print $value."<br />";}
?>


Things that I tried:
- Changed value="<?php print $id ?>" to hard-coded value="123": not work
- Use only one Submit button at the end of the form instead of the Submit links under each item: not work

Did I miss anything? or can we pass dynamically generated checkbox values?

Thanks for the inputs!

mgwalk
10-30-2008, 05:56 PM
I am having the same issue.
my dynamically created checkbox is not being posted.
looks like nobody knows about this.

thetrend
11-04-2008, 05:07 AM
I have the same problem!
I have javascript on my code but the javascript works fine, so I can't figure out the php either.

rangana
11-04-2008, 05:58 AM
It works fine. Testing example:


<?php
if(isset($_POST['submit']))
{
$compare = $_POST["compare"];
echo '<h2>Checkbox values:</h2>';
foreach ($compare as $value)
echo $value."<br />";
}
?>
<form name="input" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
for($i=1;$i<=5;$i++)
echo '<input type="checkbox" value="test'.$i.'" name="compare[]" />';
?>
<br>
<input type="submit" value="Submit" name="submit">
</form>


Checkbox/es needs to be checked to see the values.