0

So I have a calculator coded in PHP and I have validated it but there is a problem with this, its not working. I have used server side validation. Validation works well. But it doesn't do any work,for example, when I give an input like 2+8, it gives an output of 8888. This is very confusing. Please help me out with this. Thanks.

HTML page is here:

 <html>
 <head>
 <title>Calculator</title>
 </head>
 <body>
 <form method = "post" action = "calc.php">
 <input type = "text" name = "val_1"/>
 <select name="operator">
 <option>+</option>
 <option>-</option>
 <option>*</option>
 <option>/</option>
 </select>
 <input type = "text" name = "val_2"/>
 <input type = "submit" value = "calculate" name = "checker"/>
 </form>
 </body>
 </html>

And here is the PHP code.

 <?php
 if(isset($_POST['checker'])) {

 #Clean all values
 function cleanStr($str){
 $str = trim($str);
 $str = addslashes($str);
 $str = htmlspecialchars($str);
 return $str;
 }
 $val_1=cleanStr($_POST['val_1']);
 $val_2=cleanStr($_POST['val_2']);
 $operator=$_POST['operator'];

 function emptyFileds($ar){
 if(!is_array($ar)){
    echo "It must be an array";
    return false;
 }
 #loop through each field to check for empty values
 foreach($ar as $key => $value){
    $value = CleanStr($value);
    if(empty($value)){
        echo $key . " must not be empty";
        return false;
    }
    }
    return true;
    }
    if(!emptyFileds($_POST)){
    exit();
    }
    if($operator==="+"){
    echo "Sum is " . $val_1+$val_2;
    }
    }
    ?>
0

1 Answer 1

2

Please change the original line

echo "Sum is " . $val_1+$val_2;

to

echo "Sum is " . ($val_1+$val_2);

It's a operator precedence issue as . is executed first. Therefore you append 2 to "Sum is " and then increment the string by 8 which results in this odd behavior.

Also, some nitpicking on your code, I will just name 3 issues:

  1. requesting a parameter with cleanStr() is not a good idea, it's better to use

    $val_1 = (int)trim($_POST['val_1']);

    as $val_1 will be an integer after that line. This might be important for later development e.g. to compare numbers.

  2. indent correctly, reading your code is hurting my eyes

  3. the whole emptyFileds()thing is unnecessary, simply check whether the 3 parameters are filled or not, it's simple and it's readable.
Sign up to request clarification or add additional context in comments.

6 Comments

Even though this answer is correct, the use of "Try" should be omitted and implies uncertainty. You need to state confidence ;-)
i changed it to $x = $val_1+$val_2. and it worked. THanks for your help anyway.
@Fred-ii- Yes, i was not 100 percent sure whether it would solve the issue. Now i can edit my answer and remove the "Try".
It's working fine. I want to ask you guys if it's a good code or not. I mean could it be better?
And my code is always correctly indented but stack overflow's code-paste rules annoyed me so much. Thats why i didn't indent it. I'll take care of it from now on. Thanks man.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.