php is showing undefiened variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deepakNagpal
    New Member
    • Jul 2007
    • 15

    #1

    php is showing undefiened variable

    hi
    this is deepak, just beggner of learning php, facing some problem, if anyone could help me out, i will be thankful to him. when i run this code it will so me undefined variable as : calc , val1, and val2, which i have already declared in my html file.

    my html code:
    [HTML]
    <html>
    <head>
    <title>Calculat ion form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <form method="post" ACTION="calcula te.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><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="Calulate "></P>
    </form>


    </body>
    </html>
    [/HTML]
    my php code:

    [PHP]
    <?php

    $val1 = $_POST['val1'], $val2 = $_POST['val2'], $calc= &_POST['calc'];

    if(($val1 == "") || ($val2 == "") || ($calc == "")){
    header("Locatio n: http://localhost/mySite/calculate_form. html");
    exit;
    }

    if ($calc == "add"){
    $result = $val1 + $val2;

    } else if ($calc == "substruct" ){
    $result = $val1 - $val2;

    } else if ($calc == "multiply") {
    $result = $val1 * $val2;

    } else if ($calc == "divide"){
    $result = $val1 / $val2;
    }
    ?>

    <html>
    <head>
    <title>Untitl ed Document</title>
    </head>

    <body>
    <P> The result of the claculation is: <? echo "$result"; ?> </P>
    </body>
    </html>
    [/PHP]
    Last edited by ak1dnar; Jul 19 '07, 10:32 AM. Reason: Added [Code] Tags to the Codes...
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    There are some errors.Basicall y these are syntax errors.

    In Html form: (read the lines carefully)
    [HTML]<INPUT TYPE="radio" NAME="calc" VALUE="subtract ">[/HTML]

    In Php Script:

    [PHP]else if ($calc == "substruct" )[/PHP]

    And here decalre the $_POST variables like this.
    [CODE=php]$val1 = $_POST['val1'];
    $val2 = $_POST['val2'];
    $calc= $_POST['calc'];[/CODE]


    Instead of this,
    [CODE=php]$val1 = $_POST['val1'], $val2 = $_POST['val2'], $calc= &_POST['calc'];
    [/CODE]

    Comment

    • deepakNagpal
      New Member
      • Jul 2007
      • 15

      #3
      Thank very much ajaxrand, i have got the solution with your specified direction. now my script is working prperly.

      thanks

      Deepak Nagpal

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Originally posted by deepakNagpal
        Thank very much ajaxrand, i have got the solution with your specified direction. now my script is working prperly.

        thanks

        Deepak Nagpal
        Glad to hear that Deepak ! post back anytime when you have doubts.
        -Thanks!
        -Ajaxrand

        Comment

        Working...