+ 1

Beginner PHP Question - Simple Calculator

I'm trying to make a simple calculator in PHP. I have done one according to some yt tutorial which was helpful, but the calculator was quite bland. Now I'm trying to make a nicer one, so I decided to change the <select> in html for some customized css buttons. All is fine in this regard, but when I get to the php part... I have assigned each of my buttons as so: <button type="submit" name="op1" value="op1" class="myButton">+</button> <button type="submit" name="op2" value="op2" class="myButton">-</button> <button type="submit" name="op3" value="op3" class="myButton">x</button> <button type="submit" name="op4" value="op4" class="myButton">÷</button> and I tried dealing with it like this: if (isset($_GET['submit'])){ //check if any button has been pressed $nmb1 = $_GET['num1']; // get the 1st number the user wrote $nmb2 = $_GET['num2']; //get 2nd number the user wrote $opr = $_GET['op1'] || $_GET['op2'] || $_GET['op3'] || $_GET['op4']; And then I'm stuck here... Apparently variables don't work like this but I'd like for $opr value to read according to the button which has been pressed. I'm not sure how to code this and I'm assuming this is the part of the code that isn't letting it work... After that, I have a switch case to output according to the operation selected. Can anyone enlighten me with any tips as to how do I proceed? Or at least how could I phrase this for a google? I looked up some things about variables and $_GET in php but still having a hard time figuring this out.

17th Jun 2020, 6:05 PM
Allan
Allan - avatar
1 Odpowiedź
+ 2
I encountered a similar problem just recently when learning php - I think pure php is not the best choice for clickable buttons such as a calculator because it usually takes a form, collects data and works with it on a db. I encountered various solutions using ajax and some light javascript but it was a bit too hard to understand. Let me know if you find a solution though, I just decided to go with input type='number' name='num1' Select- option ( + - * /) input type='number' name='num2'
17th Jun 2020, 8:41 PM
HNNX 🐿
HNNX 🐿 - avatar