Jump to content

Help with this simple Calculator


Wiernusz

Recommended Posts

Hello. I am currently following a tutorial on building a simple calculator.

 

Here is my calculation code:

 

 

<?PHP
if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == "")) {
    header("Location: calculate_form.php");
    exit;
}
if ($_POST[calc] == "add") {
    $result = $_POST[val1] + $_POST[val2];
} else if ($_POST[calc] == "subtract") {
    $result = $_POST[val1] - $_POST[val2];
} else if ($_POST[calc] == "multiply") {
    $result = $_POST[val1] * $_POST[val2];
} else if ($_POST[calc] == "divide") {
    $result = $_POST[val1] / $_POST[val2];
}
?>

 

 

And here is my form code:

<html>

<head>

<title>Calculation Form</title>
</head>

<body>
<font size="3" face="verdana">

<FORM METHOD="post" ACTION="calc.php">
<P>Value 1: <INPUT TYPE="text" NAME="val1" SIZE=10></P>
<P>Value 2: <INPUT TYPE="text" NAME="val2" SIZE=10></P>
<P>Calculation:<br>
[<INPUT TYPE="radio" NAME="calc" VALUE="add"> <b>+</b> ]
[<INPUT TYPE="radio" NAME="calc" VALUE="subtract"> - ]
[<INPUT TYPE="radio" NAME="calc" VALUE="multiply"> <b>*</b> ]
[<INPUT TYPE="radio" NAME="calc" VALUE="divide">  <b>/</b> ]</P>

<P><INPUT TYPE="submit" NAME="submit" VALUE="Calculate"></P>
</FORM>





</body>
</html> 

 

 

It apparently works, but it also gives me these warnings, along with the correct math answer;

 

 

Notice: Use of undefined constant val1 - assumed 'val1' in E:\wamp\www\Learning\Calculator\calc.php on line 2

Notice: Use of undefined constant val2 - assumed 'val2' in E:\wamp\www\Learning\Calculator\calc.php on line 2

Notice: Use of undefined constant calc - assumed 'calc' in E:\wamp\www\Learning\Calculator\calc.php on line 2

Notice: Use of undefined constant calc - assumed 'calc' in E:\wamp\www\Learning\Calculator\calc.php on line 6

Notice: Use of undefined constant calc - assumed 'calc' in E:\wamp\www\Learning\Calculator\calc.php on line 8

Notice: Use of undefined constant val1 - assumed 'val1' in E:\wamp\www\Learning\Calculator\calc.php on line 9

Notice: Use of undefined constant val2 - assumed 'val2' in E:\wamp\www\Learning\Calculator\calc.php on line 9

 

 

 

I've put this together using a PHP 5 tutorial from a book, and have gone over it 4 times to make sure I havn't missed anything. Any help/tips would be greatly appreciated.

 

Thanks in advance!

Link to comment
Share on other sites

<?php

if (!isset($_POST['submit']) || ($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) {
    header("Location: calculate_form.php");
    exit;
}
if ($_POST['calc'] == "add") {
    $result = $_POST['val1'] + $_POST['val2'];
} else if ($_POST['calc'] == "subtract") {
    $result = $_POST['val1'] - $_POST['val2'];
} else if ($_POST['calc'] == "multiply") {
    $result = $_POST['val1'] * $_POST['val2'];
} else if ($_POST['calc'] == "divide") {
    $result = $_POST['val1'] / $_POST['val2'];
}
?>

 

isset and using ' around array indexes should solve your woes.

Link to comment
Share on other sites

$_POST[val1]

looks for a constant called val1, and would substitute in whatever value was set for that content as the key for the entry in the $_POST array

But no constant of that name has been set, so it gives you a warning to say that it's going to treat val1 as a string key

Use

$_POST['val1']

 

Link to comment
Share on other sites

Hi Wiernusz,

 

You need to check that the $_POST data are actually assigned values before you use them in that fashion.

 

$value1 = isset($_POST['val1']) ? $_POST['val1'] : "";
$value2 = isset($_POST['val2']) ? $_POST['val2'] : "";
etc....

 

Then you can write your code:

if((empty($value1)) || (empty($value2))) {
   etc..   
}

Link to comment
Share on other sites

Considering I am a PHP beginner, and this book is telling me to do things the wrong way (from the looks of it)... I am just going to scratch this whole thing, because I am just plain lost.

 

One thing I would really like to know though, is the completed code for this to actually work so I can review it, and not have these past few hours of learning be a waste.

 

 

If anyone is willing to show me the working script with no Notices/errors, I would be a happy person (for now!) :)

 

Again, thanks for the help everybody.

Link to comment
Share on other sites

Ummm....

 

<?php

if (!isset($_POST['submit']) || ($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) {
    header("Location: calculate_form.php");
    exit;
}

if ($_POST['calc'] == "add") {
    $result = $_POST['val1'] + $_POST['val2'];
} else if ($_POST['calc'] == "subtract") {
    $result = $_POST['val1'] - $_POST['val2'];
} else if ($_POST['calc'] == "multiply") {
    $result = $_POST['val1'] * $_POST['val2'];
} else if ($_POST['calc'] == "divide") {
    $result = $_POST['val1'] / $_POST['val2'];
}

echo 'My calculations returned: ' . $result;
?>

 

That would be the proper way to do it. Is that giving you an error/notice?

 

Chances are the book is just plain wrong. Programmers are not very good book writers.

Link to comment
Share on other sites

Still giving those errors. Thanks alot for the help, anyways.

 

 

I'm just going to find another place to start learning.

 

Well that may be, but I think you are saving the wrong file. The code I posted should not produce any notice errors.  My 2cents, you are saving the wrong file.

 

Here is the code I tested on my server, with no notice errors etc.

 

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
if (!isset($_POST['submit']) || ($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) {
    header("Location: calculate_form.php");
    exit;
}

if ($_POST['calc'] == "add") {
    $result = $_POST['val1'] + $_POST['val2'];
$what = " plus ";
} else if ($_POST['calc'] == "subtract") {
    $result = $_POST['val1'] - $_POST['val2'];
$what = " minus ";
} else if ($_POST['calc'] == "multiply") {
    $result = $_POST['val1'] * $_POST['val2'];
$what = " times ";
} else if ($_POST['calc'] == "divide") {
    $result = $_POST['val1'] / $_POST['val2'];
$what = " divided by ";
}

echo 'My calculations returned: ' . $_POST['val1'] . $what . $_POST['val2'] . " = " . $result;
?>

 

Which returned:

My calculations returned: 2 plus 3 = 5

Link to comment
Share on other sites

Still giving those errors. Thanks alot for the help, anyways.

 

 

I'm just going to find another place to start learning.

 

Well that may be, but I think you are saving the wrong file. The code I posted should not produce any notice errors.  My 2cents, you are saving the wrong file.

 

Here is the code I tested on my server, with no notice errors etc.

 

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
if (!isset($_POST['submit']) || ($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == "")) {
    header("Location: calculate_form.php");
    exit;
}

if ($_POST['calc'] == "add") {
    $result = $_POST['val1'] + $_POST['val2'];
$what = " plus ";
} else if ($_POST['calc'] == "subtract") {
    $result = $_POST['val1'] - $_POST['val2'];
$what = " minus ";
} else if ($_POST['calc'] == "multiply") {
    $result = $_POST['val1'] * $_POST['val2'];
$what = " times ";
} else if ($_POST['calc'] == "divide") {
    $result = $_POST['val1'] / $_POST['val2'];
$what = " divided by ";
}

echo 'My calculations returned: ' . $_POST['val1'] . $what . $_POST['val2'] . " = " . $result;
?>

 

Which returned:

My calculations returned: 2 plus 3 = 5

 

 

 

It seems as you were typing this, I edited my previous post... I did end up getting it working with your previous post.  Thanks alot! It did work :)

 

For the most part, I see what was wrong, and it's weird the publisher hadn't mentioned it. But to say the least - the book has been the best one yet (no throwing in stuff and not telling me about it!). Considering how much I've learned here compared to a whole bunch of other places, I decided I will finish this book.

 

 

Helping me fix that code taught me a few things, and props to you for taking time to help me out here. You rock!

Link to comment
Share on other sites

  • 6 years later...

I have the same book, and after spending ages trying to see if I made a type, google brought me here and ended my frustrations. The amazon reviews for this book are awful, but we're using it in class, and although there are many errors the structure of the book is pretty good, and just like you, I ended up learning something because of the errors.

 

My solution ended up working like this:

<?php
if ( ($_POST['val1']=="") || ($_POST['val2']=="") || ($_POST['calc']=="") ) {
	header("Location:calcform.php");
	exit;
}
if ($_POST['calc'] == "add") {
	$result = $_POST['val1'] + $_POST['val2'];
	}
	else if ($_POST['calc'] == "subtract") {
		$result = $_POST['val1'] - $_POST['val2'];
	}
	else if ($_POST['calc'] == "multiply") {
		$result = $_POST['val1'] * $_POST['val2'];
	}
	else if ($_POST['calc'] == "divide") {
		$result = $_POST['val1'] / $_POST['val2'];
	}
?>

<!DOCTYPE html>
<head>
<title>Calculation Result</title>
</head>
<body>
<p>The result of the calculation is <?php echo "$result"; ?>!</p>
</body>
</html>
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.