Page 1 of 1

How can I get value from form?

Posted: Fri Feb 07, 2003 12:29 pm
by Winter
Hello, everyone:
I am a newer of PHP....
My operation system is Window 2000. After I executed the files as following, some problems comes up...

--------------------------------------------------------------
first page: calculate_form.htm


<html>
<head>
<title> calculation form </title>
</head>
<body>
<form method="post" action="calculate.php">
<p>Valuel: <input type="text" name="val1" size=10></p>
<p>Value2: <input type="text" name="val2" size=10></p>

<p> Calculation:<p>

<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<br>
<p><input type="submit" name="submit" value="calculate"></p>
</form>
</body>
</html>

------------------------------------------------------------------
second page: calculate.php

<?php
if ($calc=="add") {
$result=$val1+$val2;
} else if ($calc=="subtract") {
$result=$val1-$val2;
} else if ($calc=="multiply") {
$result=$val1*$val2;
} else if ($calc=="divide") {
$result=$val1/$val2;
}

?>
<html>
<body>
<?
echo "Result is: $result";
?>
</html>
</body>

-----------------------------------------------------------------
Then there are some error messages:

Notice: Undefined variable: calc in c:\inetpub\wwwroot\phptest\calculate.php on line 2

Notice: Undefined variable: calc in c:\inetpub\wwwroot\phptest\calculate.php on line 4

Notice: Undefined variable: calc in c:\inetpub\wwwroot\phptest\calculate.php on line 6

Notice: Undefined variable: calc in c:\inetpub\wwwroot\phptest\calculate.php on line 8
fasdfa
Notice: Undefined variable: result in c:\inetpub\wwwroot\phptest\calculate.php on line 18
Result is:

-----------------------------------------------

Any idea about this error messages?
Please help!!!! I appreciate



Winter 8O

Posted: Fri Feb 07, 2003 1:00 pm
by redJag
$var1 = $_POST[$var1];

Posted: Fri Feb 07, 2003 1:06 pm
by Rob the R
Almost... you can put this at the top of your calculate.php file:

Code: Select all

$calc = $_POST['calc'] ;
$val1 = $_POST['val1'] ;
$val2 = $_POST['val2'] ;
When a form submits to a PHP file, the value of the form fields are stored in the $_POST array. See this thread for a more detailed explanation:
http://www.devnetwork.net/forums/viewtopic.php?t=511