elseif statement and operators not working?

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
User avatar
greeneel
Forum Commoner
Posts: 47
Joined: Wed Jul 30, 2003 5:19 pm

elseif statement and operators not working?

Post by greeneel »

Hi all, I have and order form that i made and in the form i have the php form script calling the processorder page e.g

<form action="processorder.php" method=post> now i think my form is ok here it is:

Code: Select all

&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;Hazels Garage&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Hazels Garage&lt;/h1&gt;
&lt;h2&gt;Order Form&lt;/h2&gt;

&lt;form action="processorder.php" method=post&gt;
&lt;table border=0&gt;
&lt;tr bgcolor=#cccccc&gt;
  &lt;td width=150&gt;Item&lt;/td&gt;
  &lt;td width=15&gt;Quantity&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;Tires&lt;/td&gt;
  &lt;td align=left&gt;&lt;input type="text" name="tireqty" size=3 maxlength=3&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;Oil&lt;/td&gt;
  &lt;td align=left&gt;&lt;input type="text" name="oilqty" size=3 maxlength=3&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;Spark Plugs&lt;/td&gt;
  &lt;td align=left&gt;&lt;input type="text" name="sparkqty" size=3 maxlength=3&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td&gt;Shipping Address&lt;/td&gt;
  &lt;td align=center&gt;&lt;input type="text" name="address" size=40 maxlength=40&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
  &lt;td colspan=2 align=center&gt;&lt;input type=submit value="Submit Order"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;  

&lt;/body&gt;
&lt;/html&gt;
Now I have been working on the processorder.php page and i basically use some operators to work out the totals and tax ifelse statements and added the fopen() to open my orders.txt file.. now my fopen script works because when i put information into the order form and submit it i`m able to see Order written , but the problems is that no matter what amounts i put into the tire, oil or sparkplug field and into the address field its not echoed to the processorder page nothing comes tru. and my totals are at $0.00.. here is the script.. could someone try it and see what i`m referring to..

Code: Select all

<html>
<head>
  <title>Hazels Garage - Order Results</title>
</head>
<body>
<h1>Hazels Garage</h1>
<h2>Order Results</h2>
<? 
   echo "<p>Order processed."; 
   echo date("H:i, jS F"); 
   echo "<br>";      
   echo "<p>Your order is as follows:"; 
   echo "<br>"; 
   echo $tireqty." tires<br>"; 
   echo $oilqty." bottles of oil<br>"; 
   echo $sparkqty." spark plugs<br>";

   define("TIREPRICE", 100);
   define("OILPRICE", 10);
   define("SPARKPRICE", 4);
  
  $totalqty = $tireqty + $oilqty + $sparkqty;
  $totalamount =  $tireqty  * TIREPRICE
                + $oilqty   * OILPRICE
                + $sparkqty * SPARKPRICE;
  $totalamount = number_format($totalamount, 2);
  echo "<br>\n";
  echo "Items ordered:       ".$totalqty."<br>\n";
  echo "Subtotal:            $".$totalamount."<br>\n";
  $taxrate = 0.10;  // local sales tax is 10%
  $totalamount = $totalamount * (1 + $taxrate);
  $totalamount = number_format($totalamount, 2);
  echo "Total including tax: $".$totalamount."<br>\n";

  if( $totalqty == 0 )
  {
    echo "You did not order anything on the previous page!<br>";
  }
  else
  {
    if ( $tireqty>0 )
      echo $tireqty." tires<br>";
    if ( $oilqty>0 )
      echo $oilqty." bottles of oil<br>";
    if ( $sparkqty>0 )
      echo $sparkqty." spark plugs<br>";
  }

  $total = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE; 
  $total=number_format($total, 2, ".", " ");
 
  echo "<P>Total of order is ".$total."</p>";
  
  echo "<P>Address to ship to is ".$address."<br>";

  $outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil\t"
                  .$sparkqty." spark plugs\t\$".$total
                  ."\t". $address."\n";

  // open file for appending
@ $fp = fopen("C:\Program Files\Apache Group\Apache2\orders\orders.txt", "a");

  flock($fp, 2); 
 
  if (!$fp)
  {
    echo "<p><strong> Your order could not be processed at this time.  "
         ."Please try again later.</strong></p></body></html>";
    exit;
  } 

  fwrite($fp, $outputstring);
  flock($fp, 3); 
  fclose($fp);

  echo "<p>Order written.</p>"; 

?>
</body>
</html>
mod_edit: added

Code: Select all

amd

Code: Select all

tags[/size]

this is what i get on the page..


<----


Hazels Garage
Order Results
Order processed.17:46, 26th August


Your order is as follows:
tires
bottles of oil
spark plugs

Items ordered: 0
Subtotal: $0.00
Total including tax: $0.00
You did not order anything on the previous page!


Total of order is 0.00

Address to ship to is 


Order written.

--->
stevehaysom
Forum Newbie
Posts: 4
Joined: Thu Aug 21, 2003 10:59 am

Post by stevehaysom »

Your register globals is Off, so that you have to get the form information from the $HTTP_POST_VARS array.

Or you could turn register globals on.

There is a function that tells you whether register globals is on, it's in the manual somewhere, something like get_register_globals_gpc() (=1 or 0) or such.

Steve
User avatar
fallen
Forum Newbie
Posts: 7
Joined: Wed Aug 27, 2003 7:46 am
Location: UK

If your globals are off, which they should be post 4.2

Post by fallen »

If your globals are off but you want to use post'ed variables in the pre 4.2 style you can use the following function to do just that.

Code: Select all

foreach($_POST as $key=>$val)&#123;

			//remove dollar sign before evaluating (bug found when submitting)
			$val=str_replace("$", "1234DOLLARSIGN1234", $val);
			//create variable name
			$newVar="$".$key;
			//assign correct value
			eval("$newVar="$val";");
			//replace dollar signs
			eval("$newVar=str_replace("1234DOLLARSIGN1234","$", $newVar);");
	&#125;
Hope that helps!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

fallen:

Easier would be

Code: Select all

extract($_POST);
at the begining of the script, then instead of $_POST['variable'] you can just reference is as normal - $varaible.

Easy
User avatar
fallen
Forum Newbie
Posts: 7
Joined: Wed Aug 27, 2003 7:46 am
Location: UK

Post by fallen »

Thats exactly what the loop did but extract is indeed a lot simple!

Cheers
Post Reply