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.

7
  • Are you getting any errors? Commented Sep 7, 2017 at 14:23
  • In what way is your code currently not working? What exactly is the problem that you're experiencing? Commented Sep 7, 2017 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. Commented Sep 7, 2017 at 14:25
  • (on a side note, your $profit value should be inside html tags <p> for example. Most browsers will show it anyway, but it's not according to w3 standards. Commented Sep 7, 2017 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. Commented Sep 7, 2017 at 14:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.