Jump to content

Very new to PHP, Can someone tell me where ive gone wrong?


ActaNonVerba1

Recommended Posts

My code works perfectly at showing my customer what they have ordered, how much it is, including tax etc. However, when i try to write to a flat file (.txt.) I get 500 errors :(

 

The issue may be with my output string or the writing to file, i dont know :/

 

Can someone help?

Thanks,

Danny.

 

CODE:

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=9">
<link rel="shortcut icon" href="favicon.png"/> 
<link href="Navigation.css" rel="stylesheet" type="text/css">
<link href="Buttons.css" rel="stylesheet" type="text/css">
<link href="CSS.css" rel="stylesheet" type="text/css">
<link href="Forms.css" rel="stylesheet" type="text/css">
<title>Welcome to Danny's Test Site - Dannys Test Site</title>
<?php
//Create Short Variable Names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$number = $_POST['number'];
$street = $_POST['street'];
$town = $_POST['town'];
$county = $_POST['county'];
$postcode = $_POST['postcode'];
$country = $_POST['country'];
$find = $_POST['find'];

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
</head>

<body>
<div id="Logo"><img src="Logo.png" width="633" height="60" alt="Logo"></div>
<div id="Navigation">
  <ul id="nav">
    <li><a href="Index.php">Home</a></li>
    <li class="current"><a href="Php.php">PHP</a>
      <ul>
        <li class="current"><a href="Php.php">Order Form</a></li>
        <li><a href="Freight.php">Freight Costs</a></li>
        <li><a href="View-Orders.php">View Orders</a></li>
      </ul>
    </li>
    <li><a href="MySQL.php">MySQL</a></li>
    <li><a href="HTML-5.php">HTML5</a></li>
    <li><a href="CSS.php">CSS3</a></li>
    <li><a href="Blog.php">Blog</a></li>
    <li><a href="Social-Networking.php">Social Networking</a></li>
  </ul>
</div>
<div id="Content">
<div id="style">
<h1> Freight Prices</h1>
<h2>Your Order is Complete</h2>
    <div id="Results">
<?php

//Time order made
echo '<br />';
echo '<p>Order Processed at ';
echo date('H:i jS F Y').'<p>';

//Order Contains
echo '<hr />';
echo '<h3> Your order is as follows: </h3>';
echo $tireqty.' Tyres<br />';
echo $oilqty.' Litres of Oil<br />';
echo $sparkqty.' Spark Plugs<br />';

//Assign Values to Variables
$totalqty= 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo 'Total number of items ordered: '.$totalqty.'<br />';
$totalamount = 0.00;

//Set Price Of Each Product
define ('TIREPRICE', 100);
define ('OILPRICE', 10);
define ('SPARKPRICE', 4);

//Total Cost Pre Tax
$totalamount = $tireqty * TIREPRICE
             + $oilqty * OILPRICE 
             + $sparkqty * SPARKPRICE;

$totalamount=number_format ($totalamount, 2, '.', ' ');

echo '<br />';
echo '<hr />';
echo '<h3>Cost</h3>';
echo 'Price without VAT: £'.number_format($totalamount,2).'<br />';

//Tax Added
$taxrate = 0.175; //VAT is 17.5%
$totalamount = $totalamount * (1 + $taxrate);
echo 'Total including VAT: £'.number_format($totalamount,2).'<br />';

//Remind user of Price
echo '<hr />';
echo '<h3> Individual Product Prices </h3>';
echo 'Tyres cost £'; echo TIREPRICE; echo ' each <br />';
echo 'Oil costs £'; echo OILPRICE; echo ' Per Litre <br />';
echo 'Spark plugs cost £'; echo SPARKPRICE; echo' each';

//Redirect user if they failed to input data
if( $totalqty == 0 )
{
header("Location: http://worldwidelighthouses.co.uk/Dannystestwebsite/error.php"); 
exit;}
//How customer found Dannys
echo '<hr />';
echo '<h3> How you found us: </h3>';
switch ($find)
{case 'a':
echo '<p> You&#39;re a loyal and regular customer! Thanks for your repeat custom! </p>';
break;
case 'b':
echo '<p> You were reffered by our TV advert! Stay tuned for more. </p>';
break;
case 'c':
echo '<p> You found us by internet advertisments, thanks for the click through!';
break;
case 'd':
echo '<p> You found out about us from a previous satisfied customer! Tell them thanks from us!';
break;	
}
echo '<br />';

//Dispatch Address
echo '<hr />';
echo '<h3> We will dispatch your order to </h3>';
echo $number.' ';
echo $street.'<br />';
echo $town.'<br />';
echo $county.'<br />';
echo $postcode.'<br />';
echo $country.'<br />';
echo '<br/>';
echo '<FORM METHOD="LINK" ACTION="php.php"><INPUT TYPE="submit" VALUE="Done! Click to Finish"></FORM><br />';

$outputstring = $date."\t".$tireqty." tires \t".$oilqty." oil \t".$sparkqty." Spark plugs\t"."They found us by".$find."\t"."Dispatch order to".$number.$street.$town.$county.$postcode.$country."\n";

//Open File for Appending
@ $fp = fopen($_SERVER['DOCUMENT_ROOT']."/orders/orders.txt", 'ab');

if (!fp)
{
echo'<p><strong>Your Order Could Not Be Processed at This Time. Please Try Again Later</strong></html>';
exit;
}

fwrite($fp, $outputstring, strlen($outputstring));
fclose($fp);

echo'<p>order written</p>';

?>
</div>
</div>
<div id="Footer">Copyright Danny Brown <?php echo date("Y"); ?></div>
</body>
</html>

Link to comment
Share on other sites

Hi

 

First thing, suspect you mean to check $fp for false after opening the file.

 

I would echo out the full file name to check it is where you expect it to be.

 

All the best

 

Keith

 

 

Thanks for the help. But as this is literally my first go, can you explain how I would go about doing what you suggest? Thanks

Link to comment
Share on other sites

Hi

 

Think you have just missed the $ sign here:-

 

@ $fp = fopen($_SERVER['DOCUMENT_ROOT']."/orders/orders.txt", 'ab');

 

if (!$fp)

{

  echo'<p><strong>Your Order Could Not Be Processed at This Time. Please Try Again Later</strong></html>';

  exit;

}

 

Also the @ operator suppresses errors. I would remove the @ for now to check the error messages it is coming back with.

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Think you have just missed the $ sign here:-

 

@ $fp = fopen($_SERVER['DOCUMENT_ROOT']."/orders/orders.txt", 'ab');

 

if (!$fp)

{

  echo'<p><strong>Your Order Could Not Be Processed at This Time. Please Try Again Later</strong></html>';

  exit;

}

 

Also the @ operator suppresses errors. I would remove the @ for now to check the error messages it is coming back with.

 

All the best

 

Keith

 

Thanks for oyur help. But i still get a 500 error.

 

You can the form etc here. http://www.worldwidelighthouses.co.uk/dannystestwebsite/Php.php

 

I think there may be an issue with the output string, can you confirm this? :)

 

Thanks,

Danny.

Link to comment
Share on other sites

Hi

 

Just tried your code and for me it appears to work. However have you checked that the file name / directory is what you expect it to be and that the directory exists?

 

All the best

 

Keith

 

Hmm, that is strange.

 

My file i wish to edit is in

 

Htdocs\DannysTestSite\Orders\Orders.txt

 

My "writefile.php"

is in

Htdocs\Dannystestsite\Writefile.php

 

 

So, does my code reflect that properly? :S

Getting Majorly confused now.

 

Thank you so much in advance for your help :) Much appreciated.

Danny.

Link to comment
Share on other sites

Hi

 

Echo out $_SERVER['DOCUMENT_ROOT']."/orders/orders.txt" and double check it is what you expect.

 

All the best

 

Keith

 

I get this.

E:\Domains\w\worldwidelighthouses.co.uk\user\htdocs/orders/orders.txt

 

Seems about right :)

 

So why do i still get error 500? :S

 

Oh wait my bad, that isnt in the DannysTestSite Folder :P

 

So is Doicument root actually the root of my server?

I thought it would be the folder my Writefile is in?

:S

Link to comment
Share on other sites

Hi

 

Echo out $_SERVER['DOCUMENT_ROOT']."/orders/orders.txt" and double check it is what you expect.

 

All the best

 

Keith

 

I get this.

E:\Domains\w\worldwidelighthouses.co.uk\user\htdocs/orders/orders.txt

 

Seems about right :)

 

So why do i still get error 500? :S

 

Oh wait my bad, that isnt in the DannysTestSite Folder :P

 

So is Doicument root actually the root of my server?

I thought it would be the folder my Writefile is in?

:S

 

Sorry to keep posting.

Ive just put the orders.txt in the location d:\ domains etc that i echo'd out.

 

And ive found it is posting to the orders.txt now as ive opened it and had a look.

However, the user using the form still gets a 500 message when they subm it the form :/

Link to comment
Share on other sites

Hi

 

Not sure why you are still getting the 500 error (assuming you meant e: not d: ).

 

Stuff I do is based on Windows servers where $_SERVER['DOCUMENT_ROOT'] is flakey. However it seems it has pointed to the root of you web server rather than the root of your site.

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Not sure why you are still getting the 500 error (assuming you meant e: not d: ).

 

Stuff I do is based on Windows servers where $_SERVER['DOCUMENT_ROOT'] is flakey. However it seems it has pointed to the root of you web server rather than the root of your site.

 

All the best

 

Keith

 

Yes i did mean E :P

And my Server is Windows based :)

I have no idea why i get 500 error either. Im instaling a local version of PHP so i cna check there.

Im only learning PHP so im not fussed about a real production environment, just wanna be able to have done it right so i can progress past chapter 2 of "PHP and MySQL Web Development" Lol.

 

Thanks a lot anyway

 

Danny.

Link to comment
Share on other sites

Hi

 

Not sure why you are still getting the 500 error (assuming you meant e: not d: ).

 

Stuff I do is based on Windows servers where $_SERVER['DOCUMENT_ROOT'] is flakey. However it seems it has pointed to the root of you web server rather than the root of your site.

 

All the best

 

Keith

 

Yes i did mean E :P

And my Server is Windows based :)

I have no idea why i get 500 error either. Im instaling a local version of PHP so i cna check there.

Im only learning PHP so im not fussed about a real production environment, just wanna be able to have done it right so i can progress past chapter 2 of "PHP and MySQL Web Development" Lol.

 

Thanks a lot anyway

 

Danny.

 

Dammit, works perfectly on my Localhost, must be a config.php issue. And i cant edit that on fast hosts :/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.