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):
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">
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/