yakoup46 Posted April 19, 2009 Share Posted April 19, 2009 <html> <title>File Test</title> <body> <form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post"> Enter Number <input type="text" name="num1"> <select> <option name="plus">+</option> <option name="sub">-</option> <option name="divid">/</option> </select> <input type="text" name="num2"> <input type="submit" value="Caclulate"> </form> <?php $num1 = @$_POST['num1']; $num2 = @$_POST['num2']; $plus = @$_POST['plus']; $sub = @$_POST['sub']; $divid = @$_POST['divid']; if(empty($num1)) echo " "; ?> </body> </html> So in this code i have a drop down menu with +,- , /. I was wondering how to have php detect whether operator i am using so that it can do the calculations? Link to comment https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/ Share on other sites More sharing options...
yakoup46 Posted April 19, 2009 Author Share Posted April 19, 2009 if($plus = TRUE) echo $num1 + $num2; else if($sub = TRUE) echo $num1 - $num2; else echo "num"; i tried that but with no results. Link to comment https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/#findComment-813852 Share on other sites More sharing options...
Daniel0 Posted April 19, 2009 Share Posted April 19, 2009 Should be like this instead: <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Enter Number <input type="text" name="num1"> <select name="operator"> <option value="plus">+</option> <option value="sub">-</option> <option value="divid">/</option> </select> <input type="text" name="num2"> <input type="submit" value="Calculate"> </form> From thereon it should be fairly simple to figure out. You can get the operator using $_POST['operator']. Link to comment https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/#findComment-813969 Share on other sites More sharing options...
yakoup46 Posted April 19, 2009 Author Share Posted April 19, 2009 I can't get it to check which operation it is using. Like +, -, /. Link to comment https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/#findComment-813984 Share on other sites More sharing options...
Daniel0 Posted April 19, 2009 Share Posted April 19, 2009 Why not? Link to comment https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/#findComment-813988 Share on other sites More sharing options...
yakoup46 Posted April 19, 2009 Author Share Posted April 19, 2009 idk. lol. i am trying. $plus = @$_POST['plus']; $sub = @$_POST['sub']; $operator = $_POST['operator']; if($operator = $plus) echo $num1 + $num2; else if($operator = $sub) echo $num1 - $num2; Link to comment https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/#findComment-813992 Share on other sites More sharing options...
Daniel0 Posted April 19, 2009 Share Posted April 19, 2009 if ($operator == 'plus') And then drop defining $plus and $sub, because these do not exist. Link to comment https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/#findComment-813995 Share on other sites More sharing options...
yakoup46 Posted April 19, 2009 Author Share Posted April 19, 2009 Why does that work. Why can't I just $plus = @$_POST['plus']; and do if($operator = $plus) if($operator == 'plus') is easier i was curious as to why the above way does not work Link to comment https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/#findComment-813998 Share on other sites More sharing options...
Daniel0 Posted April 19, 2009 Share Posted April 19, 2009 Because $_POST['plus'] doesn't exist. There is no field called "plus" in the form, but rather a field with the value "plus". There is a difference. Also, = is the assignment operator, and == is the comparison operator. It's covered in the language reference chapter in the manual. Link to comment https://forums.phpfreaks.com/topic/154765-solved-how-do-you-detect-operators/#findComment-814003 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.