8 Replies - 1019 Views - Last Post: 24 January 2013 - 05:31 PM Rate Topic: -----

#1 rneild  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-October 12

My first Php form program is showing the code as output

Posted 24 January 2013 - 01:15 PM

Hello there

I am trying to run my first piece of php code (well second) but i seem to get the php code output on the browser so i would like some advice on where i am going wrong.
Here is the code for the first form which is Orderform.html
</head>
<body>
<h1>Todd's Auto Parts</h1>
<h2>Order Form</h2>

<form action="processingorder.php" method=post>
<table border=0>
<tr bgcolor=#cccccc>
  <td width=150>Item</td>
  <td width=15>Quantity</td>
</tr>
<tr>
  <td>Tires</td>
  <td align=center><input type="text" name="tireqty" size=3 maxlength=3></td>
</tr>
<tr>
  <td>Oil</td>
  <td align=center><input type="text" name="oilqty" size=3 maxlength=3></td>
</tr>
<tr>
  <td>Spark Plugs</td>
  <td align=center><input type="text" name="sparkplugqty" size=3 maxlength=3></td>
</tr>
<tr>
  <td colspan=2 align=center><input type=submit value="Submit Order"></td>
</tr>
</table>
</form>  

</body>
</html>



this is the code for the php page called processingorder.php
<html>
<head><title>Todd's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?
echo "<P>Order Processed.";
echo date("H:i, jS F");
echo "<br>";
echo $tireqty." tires<BR>";
echo $oilqty." bottles of oil<BR>";
echo $sparkplugqty." spark plugs<BR>";
$totalqty = 0;
$totalamount = 0.00;
define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPLUGPRICE", 4);

$totalqty = $tireqty + $oilqty + $sparkplugqty;
$totalamount = $tireqty * TIREPRICE 
+ $oilqty * OILPRICE
+ $sparkplugqty * SPARKPLUGPRICE;
$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% here
$totalamount = $totalamount * (1+$taxrate);
$totalamount = number_format($totalamount, 2);
echo "total Including Tax: $".$totalamount."<BR>\n";
?>
</body>
</html>



I have just installed XAMPP and all services are running, I have managed to run a simpler php form but this one just seems to display the code after you try to enter any values or even leave null values like this:

Todd's Auto Parts
Order Results
"; echo $oilqty." bottles of oil
"; echo $sparkplugqty." spark plugs
"; $totalqty = 0; $totalamount = 0.00; define("TIREPRICE", 100); define("OILPRICE", 10); define("SPARKPLUGPRICE", 4); $totalqty = $tireqty + $oilqty + $sparkplugqty; $totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkplugqty * SPARKPLUGPRICE; $totalamount = number_format($totalamount, 2); echo "
\n"; echo "Items ordered: ".$totalqty."
\n; echo "Subtotal: ".$totalamount."
"\n; $taxrate = 0.10; // local sales tax is 10% here $totalamount = $totalamount * (1+$taxrate); $totalamount = number_format($totalamount, 2); echo "total Including Tax: $".$totalamount."
\n"; ?>

Is This A Good Question/Topic? 0
  • +

Replies To: My first Php form program is showing the code as output

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: My first Php form program is showing the code as output

Posted 24 January 2013 - 02:19 PM

The first thing to do is correct the opening tag <? to <?php.

<? is only a short-hand for echoing direct output, not the opening PHP tag - and it shouldn't really be used as it is poorly implemented, and supported, in different browsers.
Was This Post Helpful? 1
  • +
  • -

#3 rneild  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-October 12

Re: My first Php form program is showing the code as output

Posted 24 January 2013 - 02:41 PM

Okay thanks, I updated the code and i am getting a parse error
"Parse error: syntax error, unexpected 'Subtotal' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 27"

But i think that line 27 does already end with a colon?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: My first Php form program is showing the code as output

Posted 24 January 2013 - 02:52 PM

The newlines \n need to be within the quotes:

echo "Subtotal: ".$totalamount."<BR>\n";


This also applies to the previous line.
Was This Post Helpful? 0
  • +
  • -

#5 rneild  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-October 12

Re: My first Php form program is showing the code as output

Posted 24 January 2013 - 03:24 PM

Now i get undefined variable errors...

Notice: Undefined variable: tireqty in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 11
tires

Notice: Undefined variable: oilqty in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 12
bottles of oil

Notice: Undefined variable: sparkplugqty in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 13
spark plugs

Notice: Undefined variable: oilqty in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 20

Notice: Undefined variable: tireqty in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 20

Notice: Undefined variable: sparkplugqty in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 20

Notice: Undefined variable: tireqty in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 22

Notice: Undefined variable: oilqty in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 23

Notice: Undefined variable: sparkplugqty in C:\xampp\htdocs\WebProject\Ch1\processing.php on line 23

Items ordered: 0
Subtotal: 0.00
total Including Tax: $0.00
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: My first Php form program is showing the code as output

Posted 24 January 2013 - 03:52 PM

Maybe you are following an old tutorial or book that assumes the setting register_globals is ON (1) by default. It's default has changed from ON to OFF from PHP 4.2, and it is removed from PHP 5.4.

This setting essentially allowed PHP variables to be carried (persisted) between pages. This was a very bad idea in the first place?!

Anyway, you need to read the POST data - check it, sanitise it if necessary - and create variables:

if (isset($_POST['tireqty']) && !empty($_POST['tireqty'])) {
    $tireqty = $_POST['tireqty'];
}

// and if it should be a number..
if (is_numeric($_POST['tireqty'])) {
    $tireqty = intval($_POST['tireqty'], 10);
}

This post has been edited by andrewsw: 24 January 2013 - 03:53 PM

Was This Post Helpful? 1
  • +
  • -

#7 rneild  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 05-October 12

Re: My first Php form program is showing the code as output

Posted 24 January 2013 - 04:54 PM

Correct-it was an old textbook causing all of the issues...Thanks,I can confirm that it is correctly working now! I appreciate your input very much. :)

<html>
<head><title>Todd's Auto Parts - Order Results</title>
</head>
<body>
<h1>Todd's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<P>Order Processed.";
echo date("H:i, jS F");
echo "<br>";
if (isset($_POST['tireqty']) && !empty($_POST['tireqty'])) {
    $tireqty = $_POST['tireqty'];
}

// and if it should be a number..
if (is_numeric($_POST['tireqty'])) {
    $tireqty = intval($_POST['tireqty'], 10);
}
echo $tireqty." tires<BR>";
if (isset($_POST['oilqty']) && !empty($_POST['oilqty'])) {
    $oilqty = $_POST['oilqty'];
}

// and if it should be a number..
if (is_numeric($_POST['oilqty'])) {
    $oilqty = intval($_POST['oilqty'], 10);
}
echo $oilqty." bottles of oil<BR>";
if (isset($_POST['sparkplugqty']) && !empty($_POST['sparkplugqty'])) {
    $sparkplugqty = $_POST['sparkplugqty'];
}

// and if it should be a number..
if (is_numeric($_POST['sparkplugqty'])) {
    $sparkplugqty = intval($_POST['sparkplugqty'], 10);
}
echo $sparkplugqty." spark plugs<BR>";
$totalqty = 0;
$totalamount = 0.00;
define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPLUGPRICE", 4);

$totalqty = $tireqty + $oilqty + $sparkplugqty;
$totalamount = $tireqty * TIREPRICE 
+ $oilqty * OILPRICE
+ $sparkplugqty * SPARKPLUGPRICE;
$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% here
$totalamount = $totalamount * (1+$taxrate);
$totalamount = number_format($totalamount, 2);
echo "total Including Tax: $".$totalamount."<BR>\n";
?>
</body>
</html>



Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: My first Php form program is showing the code as output

Posted 24 January 2013 - 05:21 PM

Not a problem, but you need to consider what will happen when (any) one of the values is missing or invalid. You need to consider else clauses.

For this simple example though, you could perhaps just set the numbers to zero:

if (isset($_POST['tireqty']) && !empty($_POST['tireqty'])) {
    $tireqty = $_POST['tireqty'];
} else {
    $tireqty = 0;
} // etc.


BTW Your code looks quite neat :)/> which is always a good sign (in my book)!
You could always throw in a few more blank lines here and there to separate, for example, calculation-steps from echoes.

The more consecutive lines of code that don't contain a blank line, the more difficult it is to read IMO. This doesn't really apply to your current code, just a consideration in the future.

This post has been edited by andrewsw: 24 January 2013 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: My first Php form program is showing the code as output

Posted 24 January 2013 - 05:31 PM

BTWW I prefer to define constants towards the top of the page:

define("TIREPRICE", 100);
define("OILPRICE", 10);
define("SPARKPLUGPRICE", 4); // etc..


and you can embed variables within strings:

echo "Items ordered: $totalqty<br>\n";
// sometimes you will need to separate them with { }
echo "Items ordered: {$totalqty}<br>\n";


This is easier and preferable. Andy.

This post has been edited by andrewsw: 24 January 2013 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1