Jump to content

Simple PHP Calculator


echoCarlos

Recommended Posts

my first attempt at making a php script, included error handling incase the person dint pass in an integer.

 

any comments / suggestions would be highly appreciated as im still learning!

 

    
    <html>
<head>
	<title>Basic Calculator</title>
</head>
    <style type="text/css">
label
{
width: 4em;
float: left;
text-align: right;
margin-right: 0.5em;
display: block
}

.submit input
{
margin-left: 4.5em;
}
input
{
color: #781351;
background: #fee3ad;
border: 1px solid #781351
}

.submit input
{
color: #000;
background: #ffa20f;
border: 2px outset #d7b9c9
}
fieldset
{
border: 1px solid #781351;
width: 20em
}

legend
{
color: #fff;
background: #ffa20c;
border: 1px solid #781351;
padding: 2px 6px
} 

    </style>
<body>
    
     
    <form method="post" action="#">
    <fieldset>
<legend>Basic Maths Form</legend>
    <p>
    <label>First:</label>
    <input type="text" name="sum1" value="" />
    </p>
     
    <p>
    <label>Math: </label>
    <select name='calculate'>
    <option value='null'>Choose...</option>
    <option value='add'>+</option>
    <option value='multiply'>x</option>
    <option value='subtract'>-</option>
    <option value='divide'>/</option>
    </select>
    </p>
     
    <p>
    <label>Second: </label>
    <input type="text" name="sum2" value="" />
    </p>
    <b>Answere: </b> 
<?php

if(isset($_POST['sum1']) && isset($_POST['sum2']) && isset($_POST['calculate']))
{
$sum1 = strip_tags($_POST['sum1']);
$sum2 = strip_tags($_POST['sum2']);
$task = $_POST['calculate'];

if(!is_numeric($sum1) || !is_numeric($sum2))
{
	echo 'oh noes! it appears you dint enter a valid number.';
}
else
{
	switch($task)
	{
		case 'add':
			echo $sum1 + $sum2;
			break;
		case 'multiply':
			echo $sum1 * $sum2;
			break;
		case 'subtract':
			echo $sum1 - $sum2;
			break;
                       case 'divide':
                               echo $sum1 / $sum2;
                            break;
		    default:
		       echo 'OoOops!';
		       break;
	}
}
}

?>
    <p>
    <input type="submit" name="submit" value="Calculate" />
    </p>
    </fieldset>
    </form>
        
    	</body>
</html>

Link to comment
Share on other sites

heres some code i wrote awhile ago, it'll need some cleaning and you'll have to work some bits out

html:

<? if(!isset($iiX)){$iiX=1;} ?>
<form name="memnum" action="?ref=<?= $iiX; ?><?= $eXtra; ?>" enctype="multipart/form-data" method="post">
  <table align="center">
    <tr>
      <td colspan="3"><center><p><?= $title; ?></p></center></td>
    </tr>
    <tr>
      <td colspan="3">
        <? if(strtolower($kspecial) != 'password'){$kspecial = 'text';} ?>
        <input type="<?= $kspecial; ?>" name="pad" id="rfid" class="memnumfield" width="32" height="30"/>
      </td>
    </tr>    
<script>
      document.memnum.pad.focus();
      document.memnum.pad.select();
    </script>
  <?
  for($i = 1; $i <= 10; $i++)
  {
    if($i == 10 || $i == 7 || $i == 4 || $i == 1)
    { echo "<tr>"; }
    if($i == 10)
    {
      echo "
        <td>
          <input name=submitmem type=\"submit\" class=\"keypad_button\" value=\"ENTER\">
        </td>
        <td align=center>
          <input type=\"button\" class=\"keypad_button\" name=\"0\" value=\"0\" onclick=\"document.memnum.pad.value = document.memnum.pad.value+'0'\" onclick=\"goCheck(this)\">
        </td>
        <td>
          <input type=\"button\" class=\"keypad_button\" value=\"RESET\" onclick=\"document.memnum.pad.value = ''\">
        </td>
        ";
      }
      else
      {
        echo "
          <td>
            <input type=\"button\" class=\"keypad_button\" size=\"22\" name=\"".$i."\" value=\"".$i."\" onclick=\"document.memnum.pad.value = document.memnum.pad.value+'".$i."'\" onclick=\"goCheck(this)\">
          </td>
          ";
        }
        if($i == 9 || $i == 6 || $i == 3 || $i == 10){echo "</tr>";}
      }
  echo "
    <td colspan=\"3\">
	  <input type=\"button\" class=\"keypad_button\" size=\"66\" name=\"Cancel\" value=\"Cancel\" style=\"width:335px; height:35px;\" onclick=\"window.location='?ref=0'\">
    </td>
    ";
      ?>
      <tr><td colspan="3"> </td></tr>
  </table>
</form>

Link to comment
Share on other sites

  • 3 weeks later...

my first attempt at making a php script, included error handling incase the person dint pass in an integer.

 

Search for javascript form validation and php sanitize forms.  Easiest way is to just search for a sample script or plugin and edit for your needs.

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.