Jump to content

php calculator help


scifiguy

Recommended Posts

 Hello fellow coders I am having an issue coding a php calculator. My objective is to make it so that  text changes depending on the operation chosen, and the answer is in the text box. if you dont input an answer, it will give you the right answer. here is how the website is supposed to act:http://usefullittlethings.com/static/index_done.php

 
here is my code:
<!DOCTYPE html>
<html>
<head>
<title>Online Math Calculator</title>
</head>
<body>
<h1>Thank you for trying out the online math calculator.</h1>
<h2>Enter two numbers to add, subtract, multiply, or divide.</h2>
<h3>If you enter the result, I will tell you if what you entered is right.</h3>
If you don't enter both numbers for a formula, I will ignore it.<br /><br />
<?php
$status= 'Waiting for input.<br>';
echo $status;
?>
 
<?php
function Calc(){
if (isset($a1)&&!isset($a2){
if (!empty($a1)&&!empty($a2)
$a3=$a1+$a2;
echo $status.'two numbers added.'
}
}
 
?>
 
<form action="index.php" method="post">
<strong>Addition:</strong><br />
<input type="text" name="a1" value="" />
plus
<input type="text" name="a2" value="" />
equals
<input type="text" name="a3" value="" />
 
<br /><br />
<strong>Subtraction:</strong><br />
<input type="text" name="s1" value="" />
minus
<input type="text" name="s2" value="" />
equals
<input type="text" name="s3" value="" />
 
<br /><br />
<strong>Multiplication:</strong><br />
<input type="text" name="m1" value="" />
times
<input type="text" name="m2" value="" />
equals
<input type="text" name="m3" value="" />
 
<br /><br />
<strong>Division:</strong><br />
<input type="text" name="d1" value="" />
divided by
<input type="text" name="d2" value="" />
equals
<input type="text" name="d3" value="" />
 
<br /><br />
<input type="submit" value="Calculate!" />
</form>
</body>
</html>
 
Thanks. Any help is appreciated!!!!!!!!!!!!!!!!!
Link to comment
Share on other sites

WAre you unsure how to call the Calc function when someone enters values into the addition fields?

 

How I'd do it is change the addition fields to 

<strong>Addition:</strong><br />
<input type="text" name="a1" value="<?php echo field('a1'); /* get field a1 value */ ?>" />
plus
<input type="text" name="a2" value="<?php echo field('a2'); /* get field a2 value */ ?>" />
equals
<input type="text" name="a3" value="<?php echo Calc(); /* print sum */ ?>" />

Now change the Calc function to

function Calc()
{
   $num1 = field('a1');  // get the value in a1 field
   $num2 = field('a2');  // get the value in a2 field

   if (!empty( $num1) && !empty( $num2 )) // make sure values are not empty
   {
       return $num1 + $num2; // return the sum
   }
}

// helper function to get values from $_POST
function field($fieldname)
{
   return isset($_POST[$fieldname]) ? $_POST[$fieldname] : ''; // return fields value
}
Edited by Ch0cu3r
Link to comment
Share on other sites

Thank you for your help with the addition method. I tried to duplicate what you did for the other methods, but all that outputs is the sum for Addition. I tried to change the if statements to else if structure, but it doesn't compile it just says unexpected else.

Any ideas?

I also need to finish changing the text based on operation.

Thanks again.

Link to comment
Share on other sites

the code I gave was an example for just the doing the addition operation. You'll need to write new functions for doing subtraction, multiplication and division operations. You don't modify the Calc function.

 

To write the other three functions, copy and paste Calc function.

Rename it to the operation you want to do, for example Sub for subtraction.

Change field('a1') and field('a2') to so it gets the field values for the operation. So for subtraction that would be field('s1') and field('s2').

You'd change the sum to the operation,  for subtraction change return $num1 + $num2; to return $num1 - $num2;

When you want to perform the operation you'd call that function, for example the answer field for subtraction would be


<input type="text" name="s3" value="<?php echo Sub(); /* print sum */ ?>" />
Edited by Ch0cu3r
Link to comment
Share on other sites

The goal of this part is to test if the user is right if they enter something in the results box. If the user is right, The program will output you are right, otherwise, It will output you are wrong.

This is how I thought to do it, but it doesn't work:

if ($_POST['a3']==$a3){
  echo"You are right.";
  }
 { else
  echo"You are wrong.";
  }
Edited by scifiguy
Link to comment
Share on other sites

if ($_POST['a3']==$a3){   should be   if (field('a3') == Calc()){

 

Remember

The field() helper function I wrote gets the value of the field that is pass to it, for example field('a3') gets the value from the field named a3

 

Calc() adds a1 and a2 input fields and returns the sum.

 

if you are not using the field function then the if will be  if ($_POST['a3'] == Calc()){

Edited by Ch0cu3r
Link to comment
Share on other sites

That is because you have this for the answer field for addition operation

equals
<input type="text" name="a3" value="<?php echo Calc(); /* print sum */ ?>" />

When the form is submitted the Calc function is ran. And so it fills in the answer field.

 

If you only want it run if nothing has been entered into that field then change the Calc function to

function Calc()
{
	// if nothing is entered in the answer field
	// then calcute the awnser
	if(empty(field('a3')))
	{
	    $num1 = field('a1');  // get the value in a1 field
	    $num2 = field('a2');  // get the value in a2 field

	    if (!empty( $num1) && !empty( $num2 )) // make sure values are not empty
	    {
	       return $num1 + $num2; // return the sum
	    }
	}
	// something is entered in the field
	else
	{
		// return what was entered
		return field('a3');
	}
}
Edited by Ch0cu3r
Link to comment
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.