Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP Calculator (Part 2)

calculator

  • Please log in to reply
1 reply to this topic

#1 Bioshox

Bioshox

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 210 posts

Posted 07 November 2011 - 07:38 AM

Find the full tutorial here, with links and source: http://fusionstrike....lculator-part-2

Welcome to the second tutorial in a series, in this tutorial I’ll be showing you how to advance the basic calculator you created in the first tutorial (if you haven’t already, you can find that tutorial HERE), using HTML Forms we will make it able for you to enter numbers via the site, instead of editing the code when you want to add something different.Step 1:You should already have the following code in a document

<html>
<head>
<title>PHPCalc</title>
</head>
<body>
<?php
//If the submit button has been pressed, numbers have already been entered, so add them together
if(isset($_POST['submit'])){
// Numbers to add
$num1 = $_POST['num1']; $num2 = $_POST['num2'];
// Answer Variable
$answer = $num1 * $num2;
// Output
echo $num1 .’ + ‘. $num2 .’ = ‘. $answer;
}else{
?>
</body>
</html>




Step 2:We’re now going to add to that code, below the end of the PHP tag we need to add a form that we will use to insert and submit our buttons to the script with, as I said in the previous tutorial you should have basic HTML knowledge before following these tutorials, these forms are very basic.We called our file calc.php if you called yours something different you should change the file name in the form action otherwise this code shouldn’t need changing at all.

<form action=’calc.php’ method=’post’> 
<input type=”text” value=’0′ name=”num1″/>
+
<input type=”text” value=’0′ name=”num2″/>
<input type=’submit’ name=’submit’ value=’Add’ /><br/><br/><br/>
</form>




Step 3:Now we have the form created we have somewhere to enter our numbers to add them together, the only problem is we haven’t done anything with our variables, or added anything to check if the submit button has been pressed before it trys to add the numbers together, so the first thing we need to add is an IF statment to check if the submit button has been pressed, for this we use a function called ISSET, and it will check if $_POST['add'] (add is what we called the submit button) has been pressed, you should put this directly under your opening PHP tag.

//If the submit button has been pressed, numbers have already been entered, so add them together
if(isset($_POST['submit'])){




Step 4:To finish the code off we need to change the variables from numbers to the num1 and 2 text boxes from the form, we will use $_POST[''] for this, we named both text boxes num1 and num2, so that’s what we will put in the post variable

// Numbers to add 
$num1 = $_POST['num1']; $num2 = $_POST['num2'];



Your final code should now look like this, now all you have to do is save it as something.php, upload it to your web-server and see the result! I’ll be advancing this in the future so keep posted.

<html>
<head>
<title>PHPCalc</title>
</head>
<body>
<?php
//If the submit button has been pressed, numbers have already been entered, so add them together
if(isset($_POST['submit'])){
// Numbers to add
$num1 = $_POST['num1']; $num2 = $_POST['num2'];
// Answer Variable
$answer = $num1 * $num2;
// Output
echo $num1 .’ + ‘. $num2 .’ = ‘. $answer;
}else{
?>
<form action=’calc.php’ method=’post’>
<input type=”text” value=’0′ name=”num1″/>
+
<input type=”text” value=’0′ name=”num2″/>
<input type=’submit’ name=’submit’ value=’Add’ /><br/><br/><br/>
</form>
<?php
}
?>

</body>
</html>



If you need any assistance or have any questions or discussion on this feel free to ask me via the comments! You can find a working version of the tutorial here: PHPCalc


  • 0

#2 wcypierre

wcypierre

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 12 November 2011 - 04:30 AM

$answer = $num1 * $num2;
// Output
echo $num1 .’ + ‘. $num2 .’ = ‘. $answer;

Shouldn't you either change the asterisk in $answer to + or change the output to *.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download