simple calculator

Discussion in 'PHP' started by mnymkr, Apr 29, 2007.

  1. #1
    i have been studying a simple calculator and trying to implement it using just one php file instead of an html file and php file can someone point out what is wrong and why i might have thought this would be right? i am trying to use php_self

    <html><head><title>Calculation Result</title></head>
    <body>
    <?php
    if( is_numeric($val1) && is_numeric($val2) )
    {
       if($calc != null)
       {
            switch($calc)
            {
                 case "add" : $result= $val1 + $val2; break;
                 case "sub" : $result= $val1 - $val2; break;
                 case "mul" : $result= $val1 * $val2; break;
                 case "div" : $result= $val1 / $val2; break;
            }
            echo("Calculation result: $result");
        }
    }
    else{ echo("Invalid entry - please retry"); }
    
    ?>
    
    
    <form action  = "post" action = "<?php echo $PHP_SELF;?>">
    Value 1: <input type="text" name="val1" size="10">
    Value 2: <input type="text" name="val2" size="10"> <br>
    Calculation: <br>
    <input type="radio" name="calc" value="add">Add
    <input type="radio" name="calc" value="sub">Subtract
    <input type="radio" name="calc" value="mul">Multiply
    <input type="radio" name="calc" value="div">Divide <br>
    <input type="submit" value="Calculate">
    <input type="reset" value="Clear">
    </form>
    
    <?php echo number_format($result, "2", ".", ","); ?>
    
    </body></html> 
    
    
    Code (markup):
     
    mnymkr, Apr 29, 2007 IP
  2. brealmz

    brealmz Well-Known Member

    Messages:
    335
    Likes Received:
    24
    Best Answers:
    3
    Trophy Points:
    138
    #2
    theres nothing wrong with the code except for the method attribute written also as action. actually you can ignore the action attribute and just leave it like this.

    <form name="fmrname" method = "post">
     
    brealmz, Apr 29, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    do i not need the $php_self

    i thought that was what let you just use one file
     
    mnymkr, Apr 29, 2007 IP
  4. brealmz

    brealmz Well-Known Member

    Messages:
    335
    Likes Received:
    24
    Best Answers:
    3
    Trophy Points:
    138
    #4
    you can ignore that. the form will consider you were submitting to same page. your error was the method attributes. forms were defaulted to GET method. when you submit your form without POST method you will notice at your address bar that your form fields was there/
     
    brealmz, Apr 29, 2007 IP