Question : PHP for loop - help with POST data

Hi Experts,

This is my problem:

I'm posting a set of variables from one page (index.php) to a second page (confirm.php).

On the first page, I have a dynamically created set of checkboxes, the number of vars posted to the second page is not known.

When index.php is submitted, I need to capture the values from the checkboxes. I have named them "option1" through to however many, i.e. "option8".

I have ascertained that I need a for loop but how do I capture the post values from these variable checkboxes?

Thanks

Answer : PHP for loop - help with POST data

Here's a complete demo page that posts to itself and uses 'foreach' for the loop.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Check box procedure</title>
</head>
<body>
<h1>Check box procedure</h1>
<form action="checkbox.php" method="post">
<input type="checkbox" name="cbp[]" value="Beer" />Beer<br>
<input type="checkbox" name="cbp[]" value="Wine" />Wine<br>
<input type="checkbox" name="cbp[]" value="Whiskey" />Whiskey<br>
<input type="checkbox" name="cbp[]" value="Soda" />Soda<br>
<input type="checkbox" name="cbp[]" value="Ice" />Ice<br>

<input type="submit"  name="submit" value="Submit" />
</form>

<?php 
if(isset($_POST["submit"])) {
echo "<b>You have selected:</b><br>";
$choices = array();
$choices = $_POST["cbp"];
foreach($choices as $value) {
	echo $value."<br>";
	}
}
 ?>

</body>
</html>
Random Solutions  
 
programming4us programming4us