Jump to content

Recommended Posts

Hi I am brand new at all this only learned html a couple of weeks ago now trying php.

I have made a calculator for my website. The site is about carpet fitting and have made the calculator so people can get an estimate before they go to shops and buy carpet. This is the code for the form.

<html>

<head>

<title>Estimator</tltle>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

</head>

<body>

 

 

      <table style="position:absolute;top:200;left:400">

      <tr>

      <td>

      <form method="get" action="Test_Calc1.php">

      <b>width in feet:</b>

      <select name="width">

      <option>13</option>

      <option>16.6</option>

      </select>

      <br><br>

      <b>Length:</b><br>

      <input type="text" name="length">

      <br><br>

      <b>carpet price</b><br>

      <input type="text" name="price">

      <br><br>

      <input class="button" type="submit" name="sub" value="calculate" />

      <br><br>

      <div class="bottombox">The Price Is:£  <b><?php include('ans.html'); ?></b></div>

      </td>

      </tr>

      </table>

 

 

</body>

</html>

 

and this is the code for the calculator.

<?php

 

  $length = $_GET['length'];

  $width  = $_GET['width'];

  $price  = $_GET['price'];

  $ans    = $length * $width / 9 * $price;

 

 

 

  $answ = 'ans.html';

  $fh = fopen($answ, 'w');

  fwrite($fh, $ans);

  fclose($fh);

  echo ('<meta http-equiv="refresh" content="0;url=Test_Calc0.php" />');

 

?>

        Thanx for any help in advance.

The calculator works fine but I wonted to know if there are X amount of people using it at the same time would it interfear with eachother and cause trouble for me later on.

And if it would has anyone got any suggestions on how to rectify the problem.

 

Link to comment
https://forums.phpfreaks.com/topic/55579-caculator-question/
Share on other sites

YOu don't need to write the answer to a file. If two people write to the file at the same exact time, I guess they could see the same answer having put in different numbers.

 

Just echo your $ans variable on the page. That way, it will be unique for everyone.

 

You should check for blank too so you don't get divide by 0 errors or something similar.

You could use empty().

 

I would suggest using POST too. When you use GET, I do this too, but people might just type in random crap in the URL to see what happens.

 

<?php
$length = $_POST['length'];
$width  = $_POST['width'];
$price  = $_POST['price'];
$ans    = $length * $width / 9 * $price;
?>
<html>
<head>
<title>Estimator</tltle>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<table style="position:absolute;top:200;left:400">
<tr>
	<td>
		<form method="post" action="Test_Calc1.php">
			width in feet:
			<select name="width">
				<option>13</option>
				<option>16.6</option>
			</select>

			Length:
			<input type="text" name="length">
     
			Carpet price:
			<input type="text" name="price">

			<input class="button" type="submit" name="sub" value="calculate" />
     
			<div class="bottombox">The Price Is:£  <?php echo $ans; ?></div>
	</td>
</tr>
</table>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/55579-caculator-question/#findComment-274584
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.