Hello Guys, I am definitely a PHP/MySQL newbie and have an issue that I would really appreciate some help with.
I need the ability to bring up a list of products from a database (which is done.) I have all of these products listed in different checkboxes. I need the user to be able to select the products they would like to purchase, but have a way to store the selected products somewhere. The way that I was thinking (incorrectly, probably) is that I would store them in variables until I was ready to submit a confirmation summary and on the final page, store all of the values in my database.
I am OPEN to all options. If there is something easier that I haven't considered, please let me know. Below is an example of my html form with my database values being pulled into a checkbox list and the corresponding php page.
Thanks in advance.
two.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
echo "<form action='form.php' method='post'>";
[COLOR="Red"]$host="correct"; // Host name
$username="correct"; // Mysql username
$password="correct"; // Mysql password
$db_name="correct"; // Database name
$tbl_name="corect"; // Table name
$folder=$_COOKIE['folderGroup'];[/COLOR]
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE folderID = $folder";
$result=mysql_query($sql);
// Define $color=1
$color="1";
echo '<table width="500" border="0" align="left" cellpadding="2" cellspacing="0">';
while($rows=mysql_fetch_array($result)){
// If $color==1 table row color = #FFFFFF
if($color==1){
echo "<tr bgcolor='#FFFFFF'>
<td><input name='formDoor[]' type='checkbox' value='".$rows['productName']."' /></td><td><img src='folder-icon.png' /></td><td>".$rows['productName']."</td>
</tr>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='E1E1E1'>
<td><input name='formDoor[]' type='checkbox' value='".$rows['productName']."' /></td><td><img src='folder-icon.png' /></td><td>".$rows['productName']."</td>
</tr>";
// Set $color back to 1
$color="1";
}
}
echo "<tr>
<td></td>
<td></td>
<td><input name='submit' type='submit' value='Continue' /><input name='reset' type='reset' value='Clear' /></td></tr>";
echo '</table>';
echo "</form>";
mysql_close();
?>
</body>
</html>
form.php
<?php
$aDoor = $_POST['formDoor'];
if(empty($aDoor))
{
echo("You didn't select any products.");
}
else
{
$N = count($aDoor);
echo("You selected $N products(s): <br />");
for($i=0; $i < $N; $i++)
{
echo "<li>";
echo($aDoor[$i] . "<br /> ");
}
}
?>