php help

Discussion in 'OT Technology' started by Grifter, Jan 24, 2004.

  1. Grifter

    Grifter Silver Member

    Joined:
    Dec 21, 2000
    Messages:
    3,117
    Likes Received:
    0
    Location:
    the southwest
    Code:
    <?
    if (($_POST[val1] == " ") || ($_POST[val2] == " ") || ($_POST[calc] == " "))	{
    	header("Location: http://127.0.0.1/calculate.htm");
    }
    
    if ($_POST[calc] == "add")	{
    	$result = $_POST[val1] + $_POST[val2];
    }	else if ($_POST[calc] == "sub")	{
    	$result = $_POST[val1] - $_POST[val2];
    }	else if ($_POST[calc] == "mult")	{
    	$result = $_POST[val1] * $_POST[val2];
    }	else if ($_POST[calc] == "div")	{
    	$result = $_POST[val1] / $_POST[val2];
    }	else if ($_POST[calc] == "sub")	{
    	$result = $_POST[val1] - $_POST[val2];
    }	else if ($_POST[calc] == "compare")	{
    	if ($_POST[val1] > $_POST[val2])	{
    		$result = "Value 1 is greater than Value 2";
    	}	else	{
    		$result = "Value 2 is greater than Value 1";
    	}
    }	else if ($_POST[calc] == "pow")	{
    	$result = pow($_POST[val1], $_POST[val2]);
    }
    ?>
    <HTML>
    <HEAD>
    <TITLE>Calculation Result</TITLE>
    </HEAD>
    <BODY>
    <P>The result of the calculation is: <? echo "$result"; ?></P>
    </BODY>
    </HTML>
    

    gives me:

    -----------------------
    Notice: Use of undefined constant val1 - assumed 'val1' in c:\inetpub\wwwroot\calculate.php on line 2

    Notice: Use of undefined constant val2 - assumed 'val2' in c:\inetpub\wwwroot\calculate.php on line 2

    Notice: Use of undefined constant calc - assumed 'calc' in c:\inetpub\wwwroot\calculate.php on line 2

    Notice: Use of undefined constant calc - assumed 'calc' in c:\inetpub\wwwroot\calculate.php on line 6

    Notice: Use of undefined constant calc - assumed 'calc' in c:\inetpub\wwwroot\calculate.php on line 10

    Notice: Use of undefined constant calc - assumed 'calc' in c:\inetpub\wwwroot\calculate.php on line 14

    Notice: Use of undefined constant val1 - assumed 'val1' in c:\inetpub\wwwroot\calculate.php on line 16

    Notice: Use of undefined constant val2 - assumed 'val2' in c:\inetpub\wwwroot\calculate.php on line 16

    The result of the calculation is: 15

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

    when i multiply 5 and 3
     
  2. Jezza

    Jezza OT Supporter

    Joined:
    Oct 27, 2003
    Messages:
    54
    Likes Received:
    0
    Location:
    Perth Australia
    I didn't look at that very closely, but I'd guess that you're missing the $ sign in front of you're val1, val2 and calc variable names.
     
  3. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,923
    Likes Received:
    11
    Location:
    Los Angeles
    Your code actually looks fine. A few things I would change though...

    Code:
    <?
    if (($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == ""))	{
    	header("Location: http://127.0.0.1/calculate.htm");
    }
    In your original, you were testing if val1, val2, and calc were equal to an empty space. Close up the gaps into just double quotes with no space, so it will return to calculate.htm when no input is inside. You could also apply the function trim(); which will cut off all empty spaces in the front or end of the value. Not sure how lazy you are though. :p

    Code:
    }else if ($_POST[calc] == "compare")	{
    	if ($_POST[val1] > $_POST[val2])	{
    		$result = "Value 1 is greater than Value 2";
    	}	else	{
    		$result = "Value 2 is greater than Value 1";
    	}
    }
    
    Okay, here you are comparing two values, if they val1 is greater than val2, it will output the results you have. The problem here is, if both values are the same, it will output your first string "value 1 is greater than Value 2", because when both $results = false; it will use the first line or instruction available, you might want to try something like.

    Code:
    if ($val > $val2) {
         $result = "Value 1 is greater than 2";
    } else if ($val2 > $val1) {
        $result = "Value 2 is greater than Value 1";
    } else {
        $result = "Results are equal";
    }
    
    Other than that, your code should be perfectly fine.
    Also in your calculate.htm, make sure the values of the fields equal whatever you have them set in this *.php file. Multiply, subtract, add, subtract, etc.
     

Share This Page