Page 1 of 1

Need help

Posted: Thu Dec 24, 2009 8:54 am
by omerfarooq123
As i am new in php field, i write a code but giving errors..

i create calculate.html and write this..
<form method="post" action="calculate.php">

<input type="text" name="val1" size="15" /> <br></br>
<input type="text" name="val2" size="15" /><br></br>
ADD <input type ="radio" name="calc" value ="add" /><br>
Sub <input type="radio" name ="calc" value ="sub" /><br>
Multiply <input type="radio" name="calc" value="multiply" /><br>
Divide <input type="radio" name="calc" value="divide" /><br><br>
<input type="submit" name="submit" value="Calculate" />
and then create "calculate.php" and there i write this
<?php
if ($_post[calc] == "add")
{
$result = $_POST[val1] + $_POST[val2];
}


echo $result;
?>
but it is not showing when i add 2 variables.please help me

Re: Need help

Posted: Thu Dec 24, 2009 9:22 am
by omniuni
POST is a keyed array.

Code: Select all

<?php
if ($_POST[calc] == "add")
{
$result = $_POST['val1'] + $_POST['val2']; //see the quotes?
}
 
 
echo $result;
?>

Re: Need help

Posted: Thu Dec 24, 2009 9:31 am
by AbraCadaver
Variables are case sensitive. I doubt there is a $_post array, so the if will never fire.

Re: Need help

Posted: Thu Dec 24, 2009 9:40 am
by omniuni
Good Catch, Abra. I updated my code as you indicated.

Re: Need help

Posted: Thu Dec 24, 2009 10:31 am
by AbraCadaver
Update it again and quote the calc index ;-)

Re: Need help

Posted: Thu Dec 24, 2009 12:10 pm
by manohoo
The devil is in the details. Try this:

Code: Select all

 
<?php
if ($_POST["calc"] == "add")
{
$result = $_POST["val1"] + $_POST["val2"];
}
 
 
echo $result;
?>
Notice the quotes in $_POST["calc"] etc...
Also $_POST must be in upper case.

Re: Need help

Posted: Fri Dec 25, 2009 4:07 am
by omerfarooq123
Thanks a lot guyz for helping me out.. really i donot know before that $_post must be in capital Words like $_POST. Thanks