Jump to content

My first calculator


dink87522

Recommended Posts

Can I please have ome help with my first php calculator.

 

I have calculator.php

<html>
<head>
<title>PHP Calculator</title>
</head>
<body>
<h2>Calculator</h2>
<?php
// This is my first attempt at creating a simple PHP calculator
?>
<form name="form1 method="post" action="process.php">
  <label>
  <input name="answer" type="text" value="$answer">
  <br /><br />
  </label>
  <label>
  <input name="1" type="button" id="1" value="1">
   <input name="2" type="button" id="2" value="2">
   <input name="3" type="button" id="3" value="3">
   <input name="+" type="button" id="10" value="+"> 
   <br />
   <input name="4" type="button" id="4" value="4">
   <input name="5" type="button" id="5" value="5">
   <input name="6" type="button" id="6" value="6">
    <input name="-" type="button" id="11" value="-">
    <br />
   <input name="7" type="button" id="7" value="7">
   <input name="8" type="button" id="8" value="8">
   <input name="9" type="button" id="9" value="9">
   <input name="x" type="button" id="13" value="x">
   <br />
   <input name="0" type="button" id="12" value="0">
   <input name="/" type="button" id="14" value="/">
   <input name="=" type="submit" id="15" value="=">
  </label>
</form>
</body>
</html>

 

and process.php

<?php
$one = $_POST['1'];
$two = $_POST['2'];
$three = $_POST['3'];
$four = $_POST['4'];
$five = $_POST['5'];
$six = $_POST['6'];
$seven = $_POST['7'];
$eight = $_POST['8'];
$nine = $_POST['9'];
$zero = $_POST['0'];
$add = $_POST['+'];
$minus = $_POST['-'];
$multiply = $_POST['x'];
$divide = $_POST['/'];
?>

 

Link to comment
https://forums.phpfreaks.com/topic/132718-my-first-calculator/
Share on other sites

Go where with what? Programming is a very detail oriented activity. When asking a question you need to be specific. So far you have posted a form and some php code that consists of some assignment statements. I would guess you are asking how to write the code that implements a calculator in the form processing code?

Link to comment
https://forums.phpfreaks.com/topic/132718-my-first-calculator/#findComment-690208
Share on other sites

You need to compile and store what has been clicked on.  Right now if you click submit, there's nothing to send...  An easy way to do this is when a button is clicked store the values in a string delimited by a comma so now you can pass the whole string, explode it, and have case statements for your operators in process.php.

 

You could actually do it all on the same page.  Have a function that takes in a string and when submit isset call the function and return the calculation.

 

Of course you're going to have to validate, but let's get it working first.

 

Next time add some detail/research to your question.  Something like:

 

I am trying to create a basic calculator.  I have buttons in calculator.php with all the digits and operators.  I don't know how to store the values, when they are clicked, and send them to process.php so I can do the calculation.  I do not get any errors.  Any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/132718-my-first-calculator/#findComment-690512
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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