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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.