Jump to content

PHP order form quantity...no MySQL


palau

Recommended Posts

I'm a total noob, and I'm basically teaching myself PHP and MySQL, so do pardon my ignorance.  I looked through the math forum and didn't see how to do this with php only (no MySQL).  I've also looked through several reference books and still can't figure it out, so maybe I am dumb. ;)

I'm trying to make an order form using an HTML table, where the user can enter how many of a product they want to purchase.  I have no idea how to "get" the quantity entered and reference the price of the item ordered, to get a subtotal.  Annoyingly, I know how to do it with MySQL, but not with straight php. Here is my code:

 

<?php

$products[0]['brand']='Brand1";

$products[0]['price']=3.99;

$products[0]['id']=1;

.

.

.(through to all the products)

 

if (isset($_GET[$product_count])) {

$product_count=$_GET[$product_count];

}  #//actually I don't even know if I need this

 

print "<form action=\"".$_SERVER['PHP_SELF']."\" method=\"get\">\n";

print "<table class=\"center\" border=\"1\">";

print "<tr><td><h3>Product</h3></td>";

print "<td><h3>Price</h3></td>";

print "<td><h3>Quantity</h3></td></tr>";

 

foreach ($products as $product) {

$product_brand=$product['brand'];

$product_price=$product['price'];

$product_id=$product['id'];

print "<tr><td>$product_brand</td>";

print "<td>$product_price</td>";

$product_count=0;

$total_for_product=0;

$subtotal=$product_price*$product_count;

$total_for_product=$total_for_product+$subtotal;

$tax_total=0;

$tax_total=$total_for_product*.07;

$order_total=$total_for_product+$tax_total;

print "<td><input name=\"b".$product_count." size='12' type='text' value=''/></td></tr>";

print "</table>";

 

print "<input type=\"submit\" value=\"Submit Order\" name=\"submit\"/><br/>";

print "</form>";

  }

.

.

.if isset, etc......................

 

?>

 

Many thanks

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/196437-php-order-form-quantityno-mysql/
Share on other sites

A few things. One, your question isn't really a math question as what you really want to know is how to access data passed to the server form the user.

 

So lets analyze your problem:

if (isset($_GET[$product_count])) {
$product_count=$_GET[$product_count];
}  #//actually I don't even know if I need this

 

You do kind of need this, more specifically, you need to decide what you want to do when you have the count, and this is where that could go.

 

But there's a problem with your index for $_GET, take a look here:

print "<td><input name=\"b".$product_count." size='12' type='text' value=''/></td></tr>"; // you should have a closing \" after $product_count but more problems are discussed below

 

This doesn't address your problem, but is a heads up about PHP. If you use double quotes with PHP (ie. "text here") you're telling PHP that you have PHP variables inside those double quotes, and PHP needs to parse that string for PHP code.

 

When you use single quotes, your telling PHP that there's nothing but text there and PHP doesn't need to parse the string.

 

Here's a quick example:

 

<?php
$var = 'stuff';
echo "this is a string with a variable that says: $var"; // this is a string with a variable that says: stuff
echo 'this is a string with a variable that says: $var'; // this is a string with a variable that says: $var
echo 'this is a string with a variable that says: '.$var; // this is a string with a variable that says: stuff
?>

 

My point is that, when you put a string in double quotes, as you did above, and then use a period to remove the variable from the string, you're telling PHP to spend processing time (albeit a very minimal amount) checking the string for variables, but then you remove the variable from the string and add it back using concatenation which adds even MORE processing time.

 

You should either change your logic to use single quotes, and then use a period to concatenate your variable into the string, OR you should use double quotes and keep your variables inside the string so that they're parsed when PHP parses the string.

 

In any event, the problem arises with the name you have given your input field:

 

name=\"b".$product_count."\"

 

This particular name won't be very helpful to you in finding the count for each individual product.

 

You might try changing the name to:

 

name=\"product_count[$product[id]]\"

 

This will let you access the count for each product using:

 

$_GET['product_count'][$products[0]['id']]

$_GET['product_count'][$products[1]['id']]

$_GET['product_count'][$products[2]['id']]

etc.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.