Jump to content

HTML form with PHP


lucerias

Recommended Posts

I am currently create a calculator and then completed the HTML form interface with fields and labels but now i have no idea how to click on the calculate button and then it will call PHP to do the calculation and then pass back the value and display on the html form field? Please provide a simple code sample. Thank you so much.
Link to comment
https://forums.phpfreaks.com/topic/25878-html-form-with-php/
Share on other sites

this has been tested and works:
[code]
<?php  $self = $_SERVER['PHP_SELF'];
          $operator = $_POST['radio'];
          $value1 = $_POST['value1'];
          $value2 = $_POST['value2'];
if(isset($operator)){
  if($operator == "*"){
    $final_value = $value1 * $value2;
  }
  elseif($operator == "/"){
    $final_value = $value1 / $value2;
  }
  elseif($operator == "+"){
    $final_value = $value1 + $value2;
  }
  elseif($operator == "-"){
    $final_value = $value1 - $value2;
  }
} else {
    $final_value = "";
}

?>

<form method="post" action="<?php echo $self; ?>">
First Value <input type="text" name="value1" />
<br />
+ <input type="radio" name="radio" value="+" />
- <input type="radio" name="radio" value="-" />
x <input type="radio" name="radio" value="*" />
/ <input type="radio" name="radio" value="/" />
<br />
Second Value <input type="text" name="value2" />
<br />
<input type="submit" value="Calculate" />
<br /><br />
Final Value: <input type="text" value="<?php echo $final_value; ?>" />
</form>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/25878-html-form-with-php/#findComment-118229
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.