2 Replies - 157 Views - Last Post: 11 February 2019 - 02:04 AM Rate Topic: -----

#1 jjcakes   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-February 19

Help with Posting items to php order form

Posted 10 February 2019 - 11:02 AM

Hello - Having an issue with the items posting on the order summary page of the php file. I know the items are posting using a different script but when customizing the php for my purpose I lose the item array when the user selects the order item. Here is the complete code:

<!doctype html>
<html lang="en">
	
<head>
<meta charset="UTF-8">
<title>Greg's T-Shirt Shop</title>
<link rel="stylesheet" href="assignment4.css" type="text/css">
</head>

<body>
	<h1> T-Shirts For Sale</h1>
	<h2> Not for real - This is a school project</h2>
	<form action="http://mscs-php.uwstout.edu/2019sp/cs/248/001/rettlerg6800/assignment4/ordersummary.php" method="post">
	<table>
		<tr>
			<td><img src="text" alt="text"></td>
			<td>
				T-Shirt #1 Description <br>
				Price - $14.99 <br>
				<label for="tshirt1">Order Now: </label>
				<input type="checkbox" name="items[tshirt1]" value="14.99" id="tshirt1">
			</td>
			<td><img src="text" alt="text"></td>
			<td>
				T-Shirt #2 Description <br>
				Price - $19.99 <br>
				<label for="tshirt2">Order Now: </label>
				<input type="checkbox" name="items[tshirt2]" value="19.99" id="tshirt2">
			</td>
		</tr>
		<tr>
			<td><img src="text" alt="text"></td>
			<td>
				T-Shirt #3 Description <br>
				Price - $14.99 <br>
				<label for="tshirt3">Order Now: </label>
				<input type="checkbox" name="items[tshirt3]" value="14.99" id="tshirt3">
			</td>
			<td><img src="text" alt="text"></td>
			<td>
				T-Shirt #4 Description <br>
				Price - $24.99 <br>
				<label for="tshirt4">Order Now: </label>
				<input type="checkbox" name="items[tshirt4]" value="24.99" id="tshirt4">
			</td>
		</tr>
	</table>
	<fieldset>
		<legend>Order Form</legend>
		
		<label class="heading" for="firstname">First Name:</label>
		<input type="text" name="firstname" id="firstname" /><br />
		
		<label class="heading" for="lastname">Last Name</label>
		<input type="text" name="lastname" id="lastname" /><br />

		<label class="heading" for="street">Street:</label>
		<input type="text" name="street" id="street" size="40"/><br />

		<label class="heading" for="city">City:</label>
		<input type="text" name="city" id="city" /><br />

		<label class="heading" for="state">State:</label>
		<input type="text" name="state" id="state" size="2"/><br />

		<label class="heading" for="zip">Zip:</label>
		<input type="text" name="zip" id="zip" size="9" /><br />
		
		<label class="heading">Credit Card:</label>
		<label><input type="radio" name="credit" value="mc" />MasterCard</label>
		<label><input type="radio" name="credit" value="visa" />Visa</label>
		<label><input type="radio" name="credit" value="ae" />American Express</label>
	</fieldset>
	<input type="submit" />
</form>
</body>
</html>



Here is the order summary php

<!doctype html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtm1ll/DTD/xhtm1lll.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Order Summary</title>
<link rel="stylesheet" href="assignment4.css" type="text/css">
</head>

	<body>
		
	<?php
	if($_POST["items"] && $_POST["firstname"] && $_POST["lastname"] && $_POST["street"]
		&& $_POST["city"] && $_POST["state"] && $_POST["zip"])
	{
		
		$items=$POST["items"];
		$firstname=$POST["firstname"];
		$lastname=$POST["lastname"];
		$street=$POST["street"];
		$city=$POST["city"];
		$state=$POST["state"];
		$zip=$POST["zip"];
		$total=0;
		$shipping=0;
		
		
		?>
		
		
		<h1> Order Summary</h1>
		<p> Thank you <?=$firstname ?> for your order to: </p>
		<p><?= $street ?></p>
		<p><?= $city ?><?= $state ?><?= $zip ?></p>
		
		<p> You ordered the following </p>
		<table>
			<tr>
				<th>Item</th>
				<th>Price</th>
			</tr>
			
			<?php
			
				foreach($items as $item=>$price)
				{
				?>
				<tr>
					<td><?= $item ?></td>
					<td><?= $price ?></td>
				</tr>
				<?php
				$total+=$price;
				}
		
				if ($total<25) {
					$shipping=7.99;
				} elseif ($total<49) {
					$shipping=12.95;
				} else {
					$shipping=0;
				}
				$total+=$shipping;
				?>
				<tr>
					<td>Shipping</td>
					<td><?= $shipping ?></td>
				</tr>
				<tr>
					<td>Total</td>
					<td><?= $total ?></td>
				</tr>
		</table>
	
		<?php
	}
	else
	{
		?>
		<a href="Assignment4.html">Go back and complete form properly</a>
	<?php
	}
	?> 

	</body>
</html>




Is This A Good Question/Topic? 0
  • +

Replies To: Help with Posting items to php order form

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5359
  • View blog
  • Posts: 14,256
  • Joined: 18-April 07

Re: Help with Posting items to php order form

Posted 10 February 2019 - 11:08 AM

You want to be testing if they are set, not just put the $_POST in the if statement...

// Not this...

if ($_POST['blah'], $_POST['blah2']) {

// This...

if (isset($_POST['blah1'], $_POST['blah2'])) {



We want to test if the keys were set in $_POST and then if they are set, we can look at their values and go about processing the form. By putting the statements in the if statement like you are, you are not testing them properly for their existence. Give this a try and see if it helps.

Note: isset() can take multiple values and will return true if ALL are set. false otherwise.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is online

  • Stealth IT
  • member icon

Reputation: 6737
  • View blog
  • Posts: 27,740
  • Joined: 12-December 12

Re: Help with Posting items to php order form

Posted 11 February 2019 - 02:04 AM

These assignments $items=$POST["items"]; are incorrect.

name="items[tshirt1]" It also seems redundant to post these array keys unless you are posting several grouped sets of checkboxes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1