File being downloded instead of being executed

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
Booster
Forum Newbie
Posts: 8
Joined: Tue Oct 08, 2013 12:28 pm

File being downloded instead of being executed

Post by Booster »

Hi all,

I am trying to execute the html file, orderform.html, that contain the following code :

Code: Select all

<html>
<body>
<form action="processorder.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="sparkqty" size="3"
     maxlength="3"></td>
</tr>
<tr>
  <td>How did you find Bob's?</td>
  <td><select name="find">
        <option value = "a">I'm a regular customer</option>
        <option value = "b">TV advertising</option>
        <option value = "c">Phone directory</option>
        <option value = "d">Word of mouth</option>
      </select>
  </td>
</tr>
<tr>
  <td colspan="2" align="center"><input type="submit" value="Submit Order"></td>
</tr>
</table>
</form>
</body>
</html>
And after a submission of the above, the file processorder.php (please see below its content) should open in the browser (Firebox/Chrome) but instead, it is doanloaded using Firefox and the full code is displayed in google chrome instead of showing the datas submitted.

Code: Select all

<?php
  // create short variable names
  $tireqty = $_POST['tireqty'];
  $oilqty = $_POST['oilqty'];
  $sparkqty = $_POST['sparkqty'];
  $find = $_POST['find'];
?>
<html>
<head>
  <title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php

echo '<p>Order processed at ';
echo date('H:i, jS F');
echo '</p>';

echo '<p>Your order is as follows: </p>';

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo 'Items ordered: '.$totalqty.'<br />';

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 />';
}

$totalamount = 0.00;

define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);

$totalamount = $tireqty * TIREPRICE
             + $oilqty * OILPRICE
             + $sparkqty * SPARKPRICE;

echo 'Subtotal: $'.number_format($totalamount,2).'<br />';

$taxrate = 0.10;  // local sales tax is 10%
$totalamount = $totalamount * (1 + $taxrate);
echo 'Total including tax: $'.number_format($totalamount,2).'<br />';


if($find == 'a')
  echo '<p>Regular customer.</p>';
elseif($find == 'b')
  echo '<p>Customer referred by TV advert.</p>';
elseif($find == 'c')
  echo '<p>Customer referred by phone directory.</p>';
elseif($find == 'd')
  echo '<p>Customer referred by word of mouth.</p>';
else
  echo '<p>We do not know how this customer found us.</p>';


?>
</body>
</html>
Thanks in advance for your help,

Booster
tazz24mania
Forum Newbie
Posts: 10
Joined: Tue Oct 15, 2013 2:53 pm
Location: Newcastle Upon Tyne

Re: File being downloded instead of being executed

Post by tazz24mania »

This is a long shot... but have you got php enabled on your server?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: File being downloded instead of being executed

Post by Celauran »

Sounds like a PHP misconfiguration.
Booster
Forum Newbie
Posts: 8
Joined: Tue Oct 08, 2013 12:28 pm

Re: File being downloded instead of being executed

Post by Booster »

Yes, I believe that my php is properly configured as use to work with it (wampserver) since at least 6 months.

PS : I equally tried the following without success.http://serverfault.com/questions/25227/ ... f-executed
Booster
Forum Newbie
Posts: 8
Joined: Tue Oct 08, 2013 12:28 pm

Re: File being downloded instead of being executed

Post by Booster »

When I change the content of processorder.php by the code below, it works fine (each variables/datas previously provided are displayed) :

<?php

echo '<pre>';
var_export($_POST);
echo '</pre>';

?>
Booster
Forum Newbie
Posts: 8
Joined: Tue Oct 08, 2013 12:28 pm

Re: File being downloded instead of being executed

Post by Booster »

The following code works fine without the use of my debuger/ide (komodo):

Code: Select all

<html>
<head>
  <title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>


<?php

  // create short variable names
  $tireqty = $_POST['tireqty'];
  $oilqty = $_POST['oilqty'];
  $sparkqty = $_POST['sparkqty'];
  $address = $_POST['address'];

  $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
  
$date = date('H:i, jS F');

echo '<p>Order processed at ';
echo $date;
echo '</p>';

echo '<p>Your order is as follows: </p>';

$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo 'Items ordered: '.$totalqty.'<br />';

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 />';
}

$totalamount = 0.00;

define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);

$totalamount = $tireqty * TIREPRICE
             + $oilqty * OILPRICE
             + $sparkqty * SPARKPRICE;

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

echo '<p>Total of order is '.$totalamount.'</p>';
echo '<p>Address to ship to is '.$address.'</p>';

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

// open file for appending
@ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');

flock($fp, LOCK_EX); 

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, strlen($outputstring));
flock($fp, LOCK_UN); 
fclose($fp);

echo '<p>Order written.</p>'; 
?>
</body>
</html>
priyankagound
Forum Commoner
Posts: 27
Joined: Thu Sep 19, 2013 2:53 am

Re: File being downloded instead of being executed

Post by priyankagound »

Try out with this one:
well...,shared extensions (.so files), you need to enable them in php.ini and then you need to restart php.

or:
set
display_errors to On
display_startup_errors On
and notice that error_log is set to log to the syslog at the moment!

then restart apache and check again.
Booster
Forum Newbie
Posts: 8
Joined: Tue Oct 08, 2013 12:28 pm

Re: File being downloded instead of being executed

Post by Booster »

priyankagound wrote:Try out with this one:
well...,shared extensions (.so files), you need to enable them in php.ini and then you need to restart php.

or:
set
display_errors to On
display_startup_errors On
and notice that error_log is set to log to the syslog at the moment!

then restart apache and check again.
I have done this previously but the remain. In fact the problem come from my IDE (komodo): its opens externally the browser using a path like file:///C:/wamp/www/php/src/02/orderform.html while it should be localhost/php/src/02/orderform.html . I have the same issue when I directly click on the file from it folder : the only way to resolve this issue seems to manually write right path however at the moment that means I have to debug html files that redirected to php files (forms) without a debugger/ide/ .
Post Reply