0

I am new to PHP and have just started learning it. I may have gotten in over my head. I am trying to have a user input a buy value and a sell value in an html form, and then have php perform mathematical operations on those values, and finally, output the value calculated by the math operations.

<html>
<head>
 <title>PHP Test</title>
</head>
<body>

<form action="" method="post">
 Buy Price: <input type="number" name="buyprice" value="<?php echo 
 $buyprice;?>">
 Sell Price: <input type="number" name="sellprice" value="<?php echo 
 $sellprice;?>">
 <input type="submit" name="submit" value="Calculate" />
</form>

<?PHP

  $buyprice = $_POST['buyprice'];
  $sellprice = $_POST['sellprice'];
  $tax = 0.95;
  $profit = 0;

  if (isset($_POST['submit'])) { //to check if the form was submitted
  $profit = (($sellprice * $tax) - $buyprice);
  print $profit;
 }


?>


</body>
</html>

This is my attempt at it, I would appreciate any tips or ideas on how to accomplish what I want to do. Basically, the user inputs two values into the form, the php needs to use those values as variables and perform mathematical operations with them, and then I need it to output the calculated value to the page. The calculation should not be performed until the user presses the calculate button.

I'm also curious if there is a way to prevent the user from adding numbers that are negative in value (min max?). I was also wondering if there was a way to remove the arrows from the input boxes in the form.

Thanks.

  • Are you getting any errors? – Mark Baijens Sep 07 '17 at 14:23
  • In what way is your code currently *not* working? What exactly is the problem that you're experiencing? – David Sep 07 '17 at 14:24
  • Yes there are a lot of ways to check for min and maximum values. Though not directly in html attributes. But you can perform checks in Javascript, Php, etc. – Mark Baijens Sep 07 '17 at 14:25
  • (on a side note, your `$profit` value should be inside html tags `

    ` for example. Most browsers will show it anyway, but it's not according to w3 standards.

    – Mark Baijens Sep 07 '17 at 14:28
  • It seemingly takes my input in the input boxes; however, I cannot tell if it is calculating anything, and it will not output anything to my webpage at all when I press the calculate button. I'm not sure if this output problem is related to an issue with my math, or if I am just missing something. – FrenchJamesMilner Sep 07 '17 at 14:37
  • I'm not able to replicate your issue of not being able to get an output, with your code provided it fails to repopulate the textboxes but I'm still getting the calculation. – IsThisJavascript Sep 07 '17 at 14:41
  • I am using XAMPP to look at my php files on a webpage from a local directory, perhaps I have something set-up wrong? – FrenchJamesMilner Sep 07 '17 at 16:39

1 Answers1

1

Your code works, you just messed up on where to put your PHP.

<?PHP

  $buyprice = $_POST['buyprice'];
  $sellprice = $_POST['sellprice'];
  $tax = 0.95;
  $profit = 0;

  if (isset($_POST['submit'])) { //to check if the form was submitted
  $profit = (($sellprice * $tax) - $buyprice);
  print $profit;
 }


?>

<html>
<head>
 <title>PHP Test</title>
</head>
<body>

<form action="" method="post">
 Buy Price: <input type="number" name="buyprice" value="<?php echo 
 $buyprice;?>">
 Sell Price: <input type="number" name="sellprice" value="<?php echo 
 $sellprice;?>">
 <input type="submit" name="submit" value="Calculate" />
</form>

</body>
</html>      

To remove the arrows on the right, you can swap out <input type="number" for <input type="textbox".

To stop users from inputting negative numbers, you can use the pattern element for this. <input type="textbox" pattern="^[1-9][0-9]*$"

^[1-9][0-9]*$ is regex for any number above 0, however it won't accept a decimal.

If you can live with the arrows, then you can set a min property to stop input going below 0.
<input type="number" min="0"

NOTE: Even tho you are stopping users from inputting a value > 0. A malicious user may still send a fake $_POST value to your page. If you're doing anything sensitive with $_POST/$_GET/$_REQUEST you cannot trust it.

IsThisJavascript
  • 1,726
  • 2
  • 16
  • 25