problem integration php with html

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

    #1

    problem integration php with html

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

    my php code:


    <?php

    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>
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Assuming your php code is calculate.php then you need it to read the $_POST array as set when creating your form.
    Code:
    <form method="post" ACTION="calculate.php">
    Before this [PHP]if(($val1 == "") || ($val2 == "") || ($calc == "")){[/PHP] you need [PHP]$val1 = $_POST['var1']; $val2 = $_POST['val2']; $calc = $_POST['calc'];[/PHP]

    Comment

    • deepakNagpal
      New Member
      • Jul 2007
      • 15

      #3
      HI, code green

      Thanx for helping me.but i don't know why the script is not running properly, as i did as per guidence by you the php file still showing me error which i am pasting exect keywords.

      Error : PHP Notice: Undefined variable: calc in C:\Inetpub\wwwr oot\mySite\calc ulate.php on line 6 PHP Notice: Undefined variable: calc in C:\Inetpub\wwwr oot\mySite\calc ulate.php on line 9 PHP Notice: Undefined variable: calc in C:\Inetpub\wwwr oot\mySite\calc ulate.php on line 12 PHP Notice: Undefined variable: calc in C:\Inetpub\wwwr oot\mySite\calc ulate.php on line 15


      I think now you r more comfortable with my problem.

      thanks;

      Comment

      • deepakNagpal
        New Member
        • Jul 2007
        • 15

        #4
        HI, code green

        Thanx for helping me.but i don't know why the script is not running properly, as i did as per guidence by you the php file still showing me error which i am pasting exect keywords.

        Error : PHP Notice: Undefined variable: calc in C:\Inetpub\wwwr oot\mySite\calc ulate.php on line 6 PHP Notice: Undefined variable: calc in C:\Inetpub\wwwr oot\mySite\calc ulate.php on line 9 PHP Notice: Undefined variable: calc in C:\Inetpub\wwwr oot\mySite\calc ulate.php on line 12 PHP Notice: Undefined variable: calc in C:\Inetpub\wwwr oot\mySite\calc ulate.php on line 15


        I think now you r more comfortable with my problem.

        thanks;

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          I think you have not selected a radio button.
          If a radio button is not set, it's $_POST element is not created.
          Then the line
          Code:
          $calc = $_POST['calc'};
          is pointing $calc to NULL (I think) so $calc is not defined.
          Set a radio button to checked by default and it is better to test your $_POST variables first before using them.

          Comment

          Working...