How can I get value from form?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Winter
Forum Newbie
Posts: 15
Joined: Fri Feb 07, 2003 12:29 pm
Location: Texas
Contact:

How can I get value from form?

Post 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
redJag
Forum Newbie
Posts: 18
Joined: Fri Jan 31, 2003 12:17 am

Post by redJag »

$var1 = $_POST[$var1];
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post 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
Post Reply