Simple form processing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zenix
Forum Newbie
Posts: 21
Joined: Tue Nov 04, 2008 7:35 am

Simple form processing

Post by zenix »

Hi, in my quest to learn php well, I have chosen to learn from several places of info. and samples. At the moment all I am attempting to do is use a very basic HTML form and send the data to a basic php page. In all the samples I have seen (all done pretty much the same way) it works, but of course not for me. I have even gone as far as copying the code for the 2 files exactly as in one of the samples. Just to get it to work. No good. It appears the field values are not being received by my php page leading me to get the error, "Notice: Undefined index:". This error shows for all the form fields. If someone could help me out I would REALLY appreciate it so much! The code for the 2 files is below:

Code: Select all

 
 
 
<head>         
<title>Test form</title>  
</head>
<body>
Please fill out this form to calculate total <br />
<br />
 
<form action = "calc.php" method = "GET">
Price:<input type = "text" name = "price" size = "10" />
<br /> <br />
Quantity:<input type = "text" name = "quantity" size = "10" />
<br /><br />
 Tax:<input type ="text" name = "tax" size = "3" />
 <br /><br />
 Discount:<input type ="text" name = "Discount" size = "3" /> 
 Shipping method<select name ="shipping">
 <option value ="$5.00">Slow and steady</option>
 <option value ="$8.95">Put a move on it!</option>
 <option value ="$20.00">I need it yesterday!!</option>
 </select>
  <input type="hidden" name="SubmitCheck" value="sent" />
 <br />
 <br />
 Number of payments to make: <input type ="text" name = "payments" size = "3" />
 <br /><br />
 <input type = "submit" name = "submit" value = "calculate!" />
 </form>
 </body>
 </html>
The php page looks like this:
 

Code: Select all

 
<head>
<title>Calculate the input</title>
</head>
<body>
<?php  
//in case register globals is disabled
 
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$discount = $_POST['discount'];
$tax = $_POST['tax'];
$shipping = $_POST['shipping'];
$payments = $_POST['payments'];
 
//Calculate
$total = $price * $quantity;
$total = $total + $shipping;
$total = $total - $discount;
 
//determine tax rate
$taxrate = $tax/100;
$taxrate = $taxrate + 1;
 
//factor in tax
$totale = $total * $taxrate;
 
//Calculate monthly payments
$monthly = $total/payments;
 
//print results
print "You have selected to purchae: <br />
       <b>$quantity</b> Thing(s) at <br />
       $<b>$price</b>. Shipping will be
       $<b>$shipping</b> and the sales tax
       for this purchase is<b>$tax</b><br />
       After your$<b>$discount</b> discount,
       the total cost is $<b>$totale</b>. <br />
       Divided over <b>$payments</b> monthly payments
       , that would be<b>$monthly</b> each!";
?>
</body>
</html>
Is there a configuration someplace I need to deal with? It doesn't make any sense to me that it works for some people but not me. Pretty frustrating. Thanks so much in advance for your assistance!!
Last edited by zenix on Fri Nov 14, 2008 12:08 pm, edited 2 times in total.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Simple form processing

Post by chopsmith »

1. Can you provide us with a dump of the database structure?

2. I notice you have whitespace between your tag elements and their values. I'm not sure if that matters, but it's certainly unconventional. For example, I would change this:

Code: Select all

 
Price:<input type = "text" name = "price" size = "10" />
 
to this:

Code: Select all

 
Price:<input type="text" name="price" size="10" />
 
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Simple form processing

Post by chopsmith »

OHHHHH, I also just noticed that the form method you are using in the first file is GET, while the PHP file is looking for $_POST data. change $_POST to $_GET in the php (or change GET to POST in the HTML) and you should be fine.
zenix
Forum Newbie
Posts: 21
Joined: Tue Nov 04, 2008 7:35 am

Re: Simple form processing

Post by zenix »

Yeah, sorry. I was experimenting with the Get method. It was post originally. I forgot to change it. How would I run a dump on the database structure? Thank you for responding.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Simple form processing

Post by chopsmith »

Actually, forget about the database structure. That was a brain fart . . . you're not even interacting with a db here. My bad.

I notice that you are looking for $_POST['discount'], but that there is no discount input in the form. That would lead to an index doesn't exist error. Try getting rid of that from the PHP file (or adding such an input to the form), run it and let me know what happens.
zenix
Forum Newbie
Posts: 21
Joined: Tue Nov 04, 2008 7:35 am

Re: Simple form processing

Post by zenix »

Thank you, I added discount to the HTML and updated my post then ran it. It still comes up undefined index for all the fields.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Simple form processing

Post by chopsmith »

Did you get rid of all that extra space? Also, php is case-sensitive, so "Discount" and "discount" are different. Can you post here the current state of the code?
zenix
Forum Newbie
Posts: 21
Joined: Tue Nov 04, 2008 7:35 am

Re: Simple form processing

Post by zenix »

Got rid of the space you pointed out and changed it to discount from Discount. no good.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Simple form processing

Post by chopsmith »

chopsmith wrote:Can you post here the current state of the code?
zenix
Forum Newbie
Posts: 21
Joined: Tue Nov 04, 2008 7:35 am

Re: Simple form processing

Post by zenix »

I have been programming HTML for a number of years now, the white space doesn't affect the code at all....but i tried anyway, figured what the heck. What have got to lose? Thanks again! The php code is the same as already posted.

Code: Select all

<head>         
<title>Test form</title>  
</head>
<body>
Please fill out this form to calculate total <br />
<br />
 
<form action="calc.php"method="GET">
Price:<inputtype="text"name="price"size="10" />
<br /> <br />
Quantity:<input type="text"name="quantity"size="10" />
<br /><br />
 Tax:<input type="text"name="tax"size="3" />
 <br /><br />
 Discount:<input type="text"name="discount"size="3" /> 
 Shipping method<select name="shipping">
 <option value="$5.00">Slow and steady</option>
 <option value="$8.95">Put a move on it!</option>
 <option value="$20.00">I need it yesterday!!</option>
 </select>
  <input type="hidden"name="SubmitCheck"value="sent" />
 <br />
 <br />
 Number of payments to make:<input type="text"name="payments"size="3" />
 <br /><br />
 <input type ="submit"name="submit"value="calculate!" />
 </form>
 </body>
 </html>
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Simple form processing

Post by chopsmith »

doh! now you've gotten rid of too much whitespace :-)

Code: Select all

 
Quantity:<input type="text"name="quantity"size="10" />
 

Code: Select all

 
Quantity:<input type="text" name="quantity" size="10" />
 
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Simple form processing

Post by chopsmith »

This works for me (that is, I'm not getting any errors, I haven't analyzed the logic or anything):

index.php:

Code: Select all

 
<head>
<title>Test form</title>
</head>
<body>
Please fill out this form to calculate total
<br />
<br />
 
<form action="calc.php" method="GET">Price:<input type="text" name="price" size="10" /> 
<br />
<br />
Quantity:<input type="text" name="quantity" size="10" /> 
<br />
<br />
Tax:<input type="text" name="tax" size="3" /> 
<br />
<br />
Discount:<input type="text" name="discount" size="3" /> Shipping method<select name="shipping">
    <option value="$5.00">Slow and steady</option>
    <option value="$8.95">Put a move on it!</option>
    <option value="$20.00">I need it yesterday!!</option>
</select> 
<input type="hidden" name="SubmitCheck" value="sent" /> 
<br />
<br />
Number of payments to make:<input type="text" name="payments" size="3" />
<br />
<br />
<input type="submit" name="submit" value="calculate!" />
</form>
</body>
</html>
 
calc.php:

Code: Select all

 
<head>
<title>Calculate the input</title>
</head>
<body>
<?php  
 
 
$price = $_GET['price'];
$quantity = $_GET['quantity'];
$discount = $_GET['discount'];
$tax = $_GET['tax'];
$shipping = $_GET['shipping'];
$payments = $_GET['payments'];
 
//Calculate
$total = $price * $quantity;
$total = $total + $shipping;
$total = $total - $discount;
 
//determine tax rate
$taxrate = $tax/100;
$taxrate = $taxrate + 1;
 
//factor in tax
$totale = $total * $taxrate;
 
//Calculate monthly payments
$monthly = $total/$payments;
 
//print results
print "You have selected to purchae: <br />
       <b>$quantity</b> Thing(s) at <br />
       $<b>$price</b>. Shipping will be
       $<b>$shipping</b> and the sales tax
       for this purchase is<b>$tax</b><br />
       After your$<b>$discount</b> discount,
       the total cost is $<b>$totale</b>. <br />
       Divided over <b>$payments</b> monthly payments
       , that would be<b>$monthly</b> each!";
?>
</body>
</html>
 
zenix
Forum Newbie
Posts: 21
Joined: Tue Nov 04, 2008 7:35 am

Post by zenix »

Look we both know you obviously can't help me! I'll go somewhere else. My original problem had NOTHING to do with white space! You're obviously as clueless as I am.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: Simple form processing

Post by chopsmith »

Actually, I'm not, but thanks. Yes, go elsewhere
Post Reply