prathameshkakade Posted July 19, 2012 Share Posted July 19, 2012 Hello everyone.I am new to PHP please help me out. I tried to create a calculator using PHP.Its working almost fine but it is giving answers multiplide by 100 while addition. here is the code: calculator.html <html> <head> <title> Calculator </title> </head> <body> <center> <b> BASIC CALCULATOR USING PHP <br> <br> <form method="POST" action="calculator.php"> Addition: <br> <input name="numberone" type="number"> <select name="addition"> <option value="addition">+</option> </select> <input name="numbertwo" type="number"> <input type="submit" value="calculate"> <br> <br> <hr> Subraction: <br> <input name="numberthree" type="number"> <select name="subraction"> <option value="subraction">-</option> </select> <input name="numberfour" type="number"> <input type="submit" value="calculate"> <br> <br> <hr> Multiplication: <br> <input name="numberfive" type="number"> <select name="multiplication"> <option value="multiplication">*</option> </select> <input name="numbersix" type="number"> <input type="submit" value="calculate"> <br> <br> <hr> Division: <br> <input name="numberseven" type="number"> <select name="division"> <option value="division">/</option> </select> <input name="numbereight" type="number"> <input type="submit" value="calculate"> <br> <br> <hr> </form> </b> </body> </html> calculator.php <html> <head> <title> calculator </title> </head> <body> <b> Your result is: </b> <?php //addition $numberone = $_POST['numberone']; $numbertwo = $_POST['numbertwo']; $addition = $_POST['addition']; //subraction $numberthree = $_POST['numberthree']; $numberfour = $_POST['numberfour']; $subraction = $_POST['subraction']; //multiplication $numberfive = $_POST['numberfive']; $numbersix = $_POST['numbersix']; $multiplication = $_POST['multiplication']; //division $numberseven = $_POST['numberseven']; $numbereight = $_POST['numbereight']; $division = $_POST['division']; $add = ($numberone + $numbertwo); $sub = ($numberthree - $numberfour); $mul = ($numberfive * $numbersix); $div = ($numberseven / $numbereight); if ($addition) echo "$add"; if ($subraction) echo "$sub"; if ($multiplication) echo "$mul"; if ($division) echo "$div"; ?> </body> </html> Please test it and tell me errors and suggestions. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 19, 2012 Share Posted July 19, 2012 Why use PHP? Surely JS is better suited to the task. Why so many fields? Consider an interface like this: [ enter number 1 ] [ enter number 2 ] [ + ] [ - ] [ x ] [ / ] Each of the [ + ] [ - ] [ x ] [ / ] is a submit button. <input type="submit" name="operator" value="+"> <input type="submit" name="operator" value="-"> .. Quote Link to comment Share on other sites More sharing options...
prathameshkakade Posted July 19, 2012 Author Share Posted July 19, 2012 I tried doing so but it displays all the results i.e addition,subtraction,multiplication,division. What to do? Quote Link to comment Share on other sites More sharing options...
prathameshkakade Posted July 19, 2012 Author Share Posted July 19, 2012 Why use PHP? Surely JS is better suited to the task. Why so many fields? Consider an interface like this: [ enter number 1 ] [ enter number 2 ] [ + ] [ - ] [ x ] [ / ] Each of the [ + ] [ - ] [ x ] [ / ] is a submit button. <input type="submit" name="operator" value="+"> <input type="submit" name="operator" value="-"> .. I did the following after your advice calculator.html <html> <head> <title> Calculator </title> </head> <body> <b> <center> Basic calculator using PHP <br> <br> First number Second number <form method="POST" action="calculator2.php"> <input name="numberone" type="number"> <input name="numbertwo" type="number"> <br> <br> <input type="submit" name="operator" value="+ addition"> <input type="submit" name="operator" value="- subtraction"> <input type="submit" name="operatoe" value="* multiplication"> <input type="submit" name="operator" value="/ division"> </form> </center> </body> </html> <html> <head> <title> Calculator </title> </head> <body> <b> Your result is <?php $numberone = $_POST['numberone']; $numbertwo = $_POST['numbertwo']; $_POST['addition'] = $numberone + $numbertwo; $_POST['subtraction'] = $numberone - $numbertwo; $_POST['multiplication'] = $numberone * $numbertwo; $_POST['division'] = $numberone / $numbertwo; if ($_POST['operator'][0]) echo $_POST['addition']; if ($_POST['operator'][1]) echo $_POST['subtraction']; if ($_POST['operator'][2]) echo $_POST['multiplication']; if ($_POST['operator'][3]) echo $_POST['division']; ?> </b> </body> </html> Quote Link to comment Share on other sites More sharing options...
ignace Posted July 19, 2012 Share Posted July 19, 2012 Here's a very simple example using jQuery: http://jsfiddle.net/BqEDF/1/ Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted July 19, 2012 Share Posted July 19, 2012 I did the following after your advice There is so much wrong with this code. I'll try to point out everything I can see. <b> This tag is never closed, that's why the whole page is bold. First number Second number Use tables or CSS to position your labels properly, don't just space them out. They'll only look ok on your monitor. <input name="numberone" type="number"> "Number" is not a valid type for inputs. You want "text" here. <input type="submit" name="operator" value="+ addition"> <input type="submit" name="operator" value="- subtraction"> <input type="submit" name="operatoe" value="* multiplication"> <input type="submit" name="operator" value="/ division"> One of these input names is not like the others. Also, the values should not be the labels, there are multiple ways to change the text displayed on the button while still submitting basic "+", "-", "/", and "*" as the value. $_POST['addition'] = $numberone + $numbertwo; Don't ever store values in $_POST. $_POST is for raw, user-submitted content and that's all. if ($_POST['operator'][0]) echo $_POST['addition']; What makes you believe $_POST['operator'] will be an array? You never made it an array. This whole structure should be switch ( $_POST['operator'] ). The switch statement is useful. Quote Link to comment Share on other sites More sharing options...
prathameshkakade Posted July 19, 2012 Author Share Posted July 19, 2012 I did the following after your advice There is so much wrong with this code. I'll try to point out everything I can see. <b> This tag is never closed, that's why the whole page is bold. First number Second number Use tables or CSS to position your labels properly, don't just space them out. They'll only look ok on your monitor. <input name="numberone" type="number"> "Number" is not a valid type for inputs. You want "text" here. <input type="submit" name="operator" value="+ addition"> <input type="submit" name="operator" value="- subtraction"> <input type="submit" name="operatoe" value="* multiplication"> <input type="submit" name="operator" value="/ division"> One of these input names is not like the others. Also, the values should not be the labels, there are multiple ways to change the text displayed on the button while still submitting basic "+", "-", "/", and "*" as the value. $_POST['addition'] = $numberone + $numbertwo; Don't ever store values in $_POST. $_POST is for raw, user-submitted content and that's all. if ($_POST['operator'][0]) echo $_POST['addition']; What makes you believe $_POST['operator'] will be an array? You never made it an array. This whole structure should be switch ( $_POST['operator'] ). The switch statement is useful. Thank you for helping me. The main error in it is that it displays all operations example: number 1=1 number 2=2 if we click on any of the submit buttons,i get the following result 2011 i.e (addition)(subtraction)(multiplication)(division) I dont know javascript but know css.so please help me only with PHP/HTML/CSS. Thanks Quote Link to comment Share on other sites More sharing options...
prathameshkakade Posted July 19, 2012 Author Share Posted July 19, 2012 Here's a very simple example using jQuery: http://jsfiddle.net/BqEDF/1/ I dont know javascript.Please help me out using PHP/HTML/CSS only. Thanks Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 19, 2012 Share Posted July 19, 2012 <input name="numberone" type="number"> "Number" is not a valid type for inputs. You want "text" here. http://www.w3schools.com/html5/html5_form_input_types.asp I believe it is, that's how the iPhone browser and other browsers recognize fields like zipcode and automatically display the number keypad instead of text. Another source: http://stackoverflow.com/questions/593245/iphone-web-applications-and-specific-input-types Quote Link to comment Share on other sites More sharing options...
xyph Posted July 19, 2012 Share Posted July 19, 2012 You're using W3Schools to cite standards? Any browser can display any input type it wants in any way... that doesn't mean it's standard. HTML4 - http://www.w3.org/TR/html401/interact/forms.html#input-control-types HTML5 - http://www.w3.org/TR/html-markup/input.html#input So, for W3's HTML5 draft, number is indeed acceptable. He didn't declare any doctype for his markup though, so it's impossible to say if it's valid or not. Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 19, 2012 Share Posted July 19, 2012 I'm using the first thing that came up in a search for it. Your own link shows that number is a type for HTML5. I'd say it's the least important thing wrong with his program, I just felt it should be pointed out that it is a valid type. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted July 19, 2012 Share Posted July 19, 2012 I'm using the first thing that came up in a search for it. Your own link shows that number is a type for HTML5. HTML5 is not a real thing yet, it's still in draft state. Some browsers will check for these proposed types and handle them properly, but it's not technically valid as of this writing. Also, the w3c is not related to w3schools and you should not use w3schools for anything, even basic research. The actual spec has a lot of good stuff in it, including built-in validation for the "number" type. Most of it isn't in use by anyone yet, because the spec isn't finalized and is subject to change. Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 19, 2012 Share Posted July 19, 2012 Saying it's not a real thing is kind of silly. If some browsers are supporting it, how is it not real? Again, I didn't go specifically to that site, I simply googled it because I was fairly sure I've seen that type used before. I actually checked gotapi.com first but didn't see it there, so I googled. The second link I provided backs it up, and also references https://developer.apple.com/library/safari/#codinghowtos/Mobile/UserExperience/_index.html It sounds like you're saying that because one source I used should *NEVER* be used in your opinion, that invalidates the fact that it's actually quite accurate. The w3schools page itself says "Note: Not all major browsers support all the new input types. However, you can already start using them; If they are not supported, they will behave as regular text fields." which is the same thing you said. So explain to me why because you guys hate this one site, that says the same thing as many other sites, my point that it's a valid input type in HTML5 is wrong? Quote Link to comment Share on other sites More sharing options...
xyph Posted July 19, 2012 Share Posted July 19, 2012 http://w3fools.com/ W3Schools.com is not affiliated with the W3C in any way. Members of the W3C have asked W3Schools to explicitly disavow any connection in the past, and they have refused to do so. W3Schools offers certifications whose value is highly debatable? No employers recognize or respect W3Schools certificates. Unlike Microsoft?s MCP or Cisco?s CCC, W3Schools has absolutely no authority over the technologies for which they claim to provide certification. Unlike CompTIA?s ANSI/ISO accredited certifications, W3Schools has no support from governing standards bodies. W3Schools frequently publishes inaccurate or misleading content. We have collected several examples illustrating this problem below. It's a similar reason a professor would never allow someone to use Wikipedia as a credible source for information. The issue here is you definition of valid compared to ours. We consider valid to be specified as such by an accepted governing body of the specification You consider valid to be implemented by some popular application that references the specification If he had explicitly declared the document as HTML5, then according to the currect draft spec, type="number" would be valid. As it is, it's not valid according to the governing body of said specification. This is all pretty useless though. I wouldn't have bothered to join, but bad mark-up makes me angry. Quote Link to comment Share on other sites More sharing options...
Adam Posted July 20, 2012 Share Posted July 20, 2012 HTML5 is not a real thing yet, it's still in draft state. Some browsers will check for these proposed types and handle them properly, but it's not technically valid as of this writing. We consider valid to be specified as such by an accepted governing body of the specification You consider valid to be implemented by some popular application that references the specification Okay, okay. I don't think that was quite what jesirose was meaning. Nor do I think the OP intentionally didn't declare the document as HTML5. Perhaps inform him that to use the "number" type, you must technically declare the document as HTML5? Even if older browsers default to text type. Then perhaps explain what a document type is, given he's not using any and probably is just using something he saw somewhere? HTML5, whether a draft or not, is relatively widely used. Calling it "not a real thing" is insane, and discouraging someone from using I would say is wrong. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.