Form Data to Confirmation Page

I have a menu page that selects menu items from a server table. I’m using a form button to place the order and go to a confirmation page - this should insert the menu items into an order table and then display those items and quantity if the selected quantity is greater than 0. The problem I’m having is that I can’t seem to get the menu items selected into my order/confirmation page (I can’t even echo them out).

This is the menu (form starts at line 25 and code repeats at line 75):
[php]

Menu
<div id="header">
	  <h1>Thai Cuisine</h1>
	  <h2>Welcome!</h2>
</div>
	<div id="tabs">
		<ul>
			<li><a href="index.html">Home</a></li>
			<li><a href="menu.php">Menu</a></li>
			<li><a href="signin.php">My Account</a></li>
		</ul>
	</div>

<div id="main">
<?php $link = mysqli_connect("localhost","jmckenzi_jmckenz","mck3nzi3","jmckenzi_customer") or die("Error " . mysqli_error($link));
	echo "<table border='1' cellpadding='1' cellspacing='1'>";
	echo "<tr>";
	echo "<th>Quantity</th>";
	echo "<th>Name</th>";
	echo "<th>Description</th>";
	echo "<th>Spicy?</th>";
	echo "<th>Price</th>";
	echo "</tr>	";

//echo APPETIZERS to html table
$queryApp = “SELECT * FROM menu WHERE itemCat = ‘APPETIZERS’” or die(“Error in the consult…” . mysqli_error($link));
$resultApp = $link->query($queryApp);
echo “

”;
echo"
";
echo (“APPETIZERS”);
		if ($resultApp->num_rows > 0) {
		// output data of each row
			while($row = $resultApp->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>							 
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				/*testing quantity as text input - no change in result; order script shows 0 results
				echo "<td><input type='text' name='quantity'/></td>";*/
				echo "<td name='itemName'>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td name='isSpicy' style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td name='itemPrice' style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

//echo SOUPS the html table
$querySou = “SELECT * FROM menu WHERE itemCat = ‘SOUPS’” or die(“Error in the consult…” . mysqli_error($link));
$resultSou = $link->query($querySou);
echo “

”;
echo"
";
echo (“SOUPS”);
		if ($resultSou->num_rows > 0) {
		// output data of each row
			while($row = $resultSou->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				echo "<td>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

//echo SALADS the html table
$querySal = “SELECT * FROM menu WHERE itemCat = ‘SOUPS’” or die(“Error in the consult…” . mysqli_error($link));
$resultSal = $link->query($querySal);
echo “

”;
echo"
";
	if ($resultSal->num_rows > 0) {
	// output data of each row
		while($row = $resultSal->fetch_assoc()) {
			echo "<tr>";
			printf ("<td align ='center'>
					<select>
						<option value='0' name='quantity'>0</option>
						<option value='1' name='quantity'>1</option>
						<option value='2' name='quantity'>2</option>
						<option value='3' name='quantity'>3</option>
						<option value='4' name='quantity'>4</option>
						<option value='5' name='quantity'>5</option>
						<option value='6' name='quantity'>6</option>
						<option value='7' name='quantity'>7</option>
						<option value='8' name='quantity'>8</option>
						<option value='9' name='quantity'>9</option>
					</select>
				</td>");
			echo "<td>" . $row["itemName"] . "</td>"; 
			echo "<td>" . $row["itemDesc"] . "</td>";  
			echo "<td style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
			echo "<td style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
			echo "</tr>";
		}
	} 
	else {
		echo "0 results";
	}
	echo "</div>";

//echo NOODLES the html table
$queryNoo = “SELECT * FROM menu WHERE itemCat = ‘NOODLES’” or die(“Error in the consult…” . mysqli_error($link));
$resultNoo = $link->query($queryNoo);
echo “

”;
echo"
";
echo (“NOODLES”);
		if ($resultNoo->num_rows > 0) {
		// output data of each row
			while($row = $resultNoo->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				echo "<td>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

//echo RICE DISHES the html table
$queryRic = “SELECT * FROM menu WHERE itemCat = ‘SOUPS’” or die(“Error in the consult…” . mysqli_error($link));
$resultRic = $link->query($queryRic);
echo “

”;
echo"
";
echo (“RICE DISHES”);
		if ($resultRic->num_rows > 0) {
		// output data of each row
			while($row = $resultRic->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				echo "<td>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

//echo CURRY the html table
$queryCur = “SELECT * FROM menu WHERE itemCat = ‘CURRY’” or die(“Error in the consult…” . mysqli_error($link));
$resultCur = $link->query($queryCur);
echo “

”;
echo"
";
echo (“CURRY”);
		if ($resultCur->num_rows > 0) {
		// output data of each row
			while($row = $resultCur->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				echo "<td>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

//echo ENTREES the html table
$queryEnt = “SELECT * FROM menu WHERE itemCat = ‘ENTREES’” or die(“Error in the consult…” . mysqli_error($link));
$resultEnt = $link->query($queryEnt);
echo “

”;
echo"
";
echo (“ENTREES”);
		if ($resultEnt->num_rows > 0) {
		// output data of each row
			while($row = $resultEnt->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				echo "<td>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

//echo SEAFOOD SPECIALTIES the html table
$querySea = “SELECT * FROM menu WHERE itemCat = ‘SEAFOOD SPECIALTIES’” or die(“Error in the consult…” . mysqli_error($link));
$resultSea = $link->query($querySea);
echo “

”;
echo"
";
echo (“SEAFOOD SPECIALTIES”);
		if ($resultSea->num_rows > 0) {
		// output data of each row
			while($row = $resultSea->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				echo "<td>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

//echo BEVERAGES the html table
$queryBev = “SELECT * FROM menu WHERE itemCat = ‘BEVERAGES’” or die(“Error in the consult…” . mysqli_error($link));
$resultBev = $link->query($queryBev);
echo “

”;
echo"
";
echo (“BEVERAGES”);
		if ($resultBev->num_rows > 0) {
		// output data of each row
			while($row = $resultBev->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				echo "<td>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

//echo DESSERTS the html table
$queryDes = “SELECT * FROM menu WHERE itemCat = ‘DESSERTS’” or die(“Error in the consult…” . mysqli_error($link));
$resultDes = $link->query($queryDes);
echo “

”;
echo"
";
echo (“DESSERTS”);
		if ($resultDes->num_rows > 0) {
		// output data of each row
			while($row = $resultDes->fetch_assoc()) {
				echo "<tr>";
				printf ("<td align ='center'>
						<select>
							<option value='0' name='quantity'>0</option>
							<option value='1' name='quantity'>1</option>
							<option value='2' name='quantity'>2</option>
							<option value='3' name='quantity'>3</option>
							<option value='4' name='quantity'>4</option>
							<option value='5' name='quantity'>5</option>
							<option value='6' name='quantity'>6</option>
							<option value='7' name='quantity'>7</option>
							<option value='8' name='quantity'>8</option>
							<option value='9' name='quantity'>9</option>
						</select>
					</td>");
				echo "<td>" . $row["itemName"] . "</td>"; 
				echo "<td>" . $row["itemDesc"] . "</td>";  
				echo "<td style='text-align:center'>" . $row["isSpicy"] . "</td>"; 
				echo "<td style='text-align:right'>" .'$'. $row["itemPrice"] . "</td>";
				echo "</tr>";
			}
		} 
		else {
			echo "0 results";
		}
	echo "</div>";

echo “”;

		//close connection
		$link->close();	
	?>
[/php]

And this is the order/confirmation page:
[php]<?php
include “scripts/database_connection.php”;

$itemPrice = $_REQUEST['itemPrice']; 		//Retrieve data from form and store as variables
$quantity = $_REQUEST['quantity']; 
$itemName = $_REQUEST['itemName']; 

$query = "INSERT INTO order(itemPrice,quantity,itemName) VALUES('$itemPrice','$quantity','$itemName')";// WHERE '$quantity' > 0";
$result = mysqli_query($link, $query);

if ($result->num_rows > 0) {
	// output data of each row
	while($row = $result->fetch_assoc()) {
		$last_inserted_id = $link->insert_id; 
		$affected_rows = $link->affected_rows; 
		echo("Last Inserted Id: ". $last_inserted_id ); 
		echo("</br>Affected rows: " . $affected_rows); 
		echo("</br></br>Successfully added to the database:"); 
		echo("<table>
		<tr><td>Price:</td><td>{$itemPrice}</td></tr>
		<tr><td>Quantity:</td><td>{$quantity}</td></tr>
		<tr><td>Item:</td><td>{$itemName}</td></tr>
		</table>");						
	}
} else {
	echo "0 results";
}
  
$link->close(); 						//Close connection
echo("</br>Go back to <a href ='/menu.php'>Home</a>"); 

?>[/php]

Any guidance would be greatly appreciated.

Jason, isn’t this a duplicate of your other post? I responded to that one. I will close this one as solved.
Please just use on post. Duplicates are not really allowed.

Thanks, Ernie

Ernie, I’d appreciate it if you would post your reply to this post. I know i originally posted elsewhere, but I learned shortly after how to format it better. I checked my profile page for all posts and responses, but that other post and your response doesn’t appear.

No problem…

The first issue is that you have SELECT clause that does not post to the form.
This is how a SELECT works. You create a clause. You name it something.
… Then, you add various clauses to it, each with a
value of some sort. When you read the posted option that was select, you have to remember that
you are reading the “SELECT”, not the option. The value of the option that the user selects will be
passed under the select’s name. In my example it is menu1. Therefore to read the option that the
user selected, you would use $_POST[‘menu1’] and you would get the value of the option they did
select. Hope that makes sense to you.

Therefore, to fix this, remove all of your names inside the clauses. ( name=‘quantity’ )
And, place it into the one … Then, you can read the option they pick.

Now, another issue is that you created a list of quantity SELECT’s using a loop thru a list from your
database. If all of the SELECT’s are named the same, it might be hard for you to tell which are which.
You might want to rethink how you are creating each row of data that the user selects from. It might
be easier to use your loop to create a name for the select that is programmed. Such as using the ID
number of the item and using a name like " name=‘quantity13’ " " name=‘quantity35’ " where the
ID number is after the quantity for the item for that row. It is a bit trickier to loop them when reading
them back into the order code, but, in that way you can know that one “quantity” value is for one item.
Not sure if I explained that clear enough.

Another way to explain… Let’s say two SELECT’s with two OPTIONS:

1 2 1 2 These are on two different rows of a table in the display that the user sees. They select 1 for the first and they select 2 for the next. How do YOU tell which items they selected from? There are hundreds of ways to handle this. You could for instance code the values of the quantities with item number and quantity, such as if the two items were #139 and #153 or whatever you use for your item ID numbers, you could use values in the options like value="139-1" and value="139-2" and then decode them in the order area, but that is a bit confusing. I would rather change the names of the SELECT clauses. So the first one would be maybe for the first item ID number. In that way, you can parse thru the POST'd array and locate all select names that start with "quantity" and you would already have the item number and the value of the OPTION would be inside that array string.

I am sure this has caused you to do further thinking. Once you UN-NAME your options and name your
selects, I think you will see you need to rethink your process. Let us know when you get stuck at the
next step. Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service