Jump to content

Recommended Posts

hey i'm completely new and would like some expert advice. though my problem is probably relatively a simple one I can't seem to get this calculator working. I was hoping to get some help or at least advice on what i am doing wrong here. I've pasted the code I have below. I have been trying to get it work the past couple hours...sigh...i hope someone here can at least point me in the right direction. Thanks.

 

<html>

<head>
<meta http-equiv="Content-Language" content="en-ca">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Simple Calculator</title>
</head>

<body>

<form method="POST" action="LabO2.php">
<p align="center"><b>Number 1: </b><input type="text" name="number1" size="10"></p>
<p align="center"><b>Number 2: </b> <input type="text" name="number2" size="10"></p>
<p align="center"><b>Result: </b> <?php echo ($result); ?>

<p align="center">
<input type="submit" value="+" name="func"> 
<input type="submit" value="-" name="func">
<input type="submit" value="*" name="func">
<input type="submit" value="/" name="func">

<?php

if($do == "+"){
$result = $POST['number1' + $POST['number2'];
}
if($do == "-"){
$result = $POST['number1' - $POST['number2'];
}
if($do == "*"){
$result = $POST['number1' * $POST['number2'];
}
if($do == "/"){
$result = $POST['number1' / $POST['number2'];
}

?>
</p>

<p align="center"><input type="reset" value="Clear"></p>

</form>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/90196-need-some-helpprobably-a-simple-problem/
Share on other sites

Gotta add some error handlers:

 

<?php

$_POST['number1'] = $_POST['number1']*1;
$_POST['number2'] = $_POST['number2']*1;

if(is_numeric($_POST['number1']) && is_numeric($_POST['number2']){//if both are numeric

if($do == "+"){
$result = $POST['number1'] + $POST['number2'];
}
if($do == "-"){
$result = $POST['number1'] - $POST['number2'];
}
if($do == "*"){
$result = $POST['number1'] * $POST['number2'];
}
if($do == "/"){
$result = $POST['number1'] / $POST['number2'];
}

} else {//at least one is not numeric

die("One of your entries is not a valid number! Please try again.");

}

 

This will multiply the user submissions by 1 (automatically converting from string to number if plausible). The last thing I added was brackets around $_POST['number1'] in the $result areas, as you forgot that second bracket.

I actually changed the $do to $func on there so I know they are mismatched. I pasted a copy of the code while I was editiing it by mistake...the problem remains the same I can't print the results. Ideally I'd want the printed on the same form in a textbox beside Result: but right now I would just settle for it to show up at all.

It may be the multiple submits, try using a dropdown instead:

 

<select name="do">
<option value="+">Add</option>
<option value="-">Subtract</option>
<option value="*">Multiply</option>
<option value="/">Divide</option>
</select>
<input type="submit">

Now that we've fixed your coding flaws, let's fix the logical flaw. We're going to check to see if it was posted, if it was, handle everything, THEN echo (don't just echo then handle everything..)

 

<?php
if($_POST && !empty($_POST)){

//insert the do/func functions for adding multiplying and subtracting

} else {$result = null;}//nullify if they didn't post

?>

//HTML IN HERE

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.