Jump to content

Newbie needs Help


amavadia

Recommended Posts

Hi,

 

I've just started to learn php today from a book, and ive created this example for a calculator but i keep getting an error message saying there is an unidentified variable.

 

There is a html page with the forms on it:

 


<HTML>
<HRAD>
<TITLE>Caluclation Form</TITLE>
</HEAD>
<BODY>

<FORM METHOD="POST" ACTION="calculate.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"> add<br>
<INPUT TYPE="radio" NAME="calc" VALUE="subtract"> subtract<br>
<INPUT TYPE="radio" NAME="calc" VALUE="multiply"> multiply<br>
<INPUT TYPE="radio" NAME="calc" VALUE="divide"> divide</p>

<p><INPUT TYPE="submit" NAME="submit" VALUE="Calculate"></p>

</FORM>
</BODY>
</HTML>

 

and the php script in a nother file:

 


<php
if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == "")) {

header("Location: http://127.0.0.1/calculate_form.html");

}


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];
}


?>

<HTML>
<HEAD>
<TITLE>Calculation Result</TITLE>
</HEAD>
<BODY>

<p>The result of the calculation is: <?php echo "$result"; ?></p>

</BODY>
</HTML>

 

When you don't enter anything into the calc, it is meant to reload the page, but i get this:

 

The result of the calculation is:

Notice: Undefined variable: result in C:\wamp\www\calculate.php on line 28

 

 

 

Can anyone see what the problem is? Probably something really obvious but im stumped  :-\

 

Thanks!

 

 

 

Link to comment
Share on other sites

You also have other options that might be better to use, but it real depends on what you are trying to do.

You also don't need the extra brackets "()".

 

This

<?php
if ($_POST['val1'] == "" || $_POST['val2'] == "" || $_POST['calc'] == "") {
header("Location: http://127.0.0.1/calculate_form.html");
}
?>

 

Or This

<?php
if (empty($_POST['val1']) || empty($_POST['val2']) || empty($_POST['calc'])) {
header("Location: http://127.0.0.1/calculate_form.html");
}
?>

 

Or This

<?php
if (!isset($_POST['val1']) || !isset($_POST['val2']) || !isset($_POST['calc'])) {
header("Location: http://127.0.0.1/calculate_form.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.