LiamProductions Posted July 6, 2007 Share Posted July 6, 2007 Hey, i don't want you to re write the whole program, but just fix the error or tell me what the error is: I am making a Addition Form. The problem is use it, on the addition.php the answer always comes out as 0... the answer should be coming from maths.php here are the codes: Maths.php <html> <head> <title>Maths Addition</title> </head> <body bgcolor="black"> <font color="white"> <center><h1>Addition Form</h1> <form method="post" action="addition.php"> <input type="text" name="x" value="0"> <font color="red"><b>+</b></font> <input type="text" name="y" value="0"> <br /><br /> <input type="submit" value="Do the addition!"> </form> <br /><br /><br /><br /><br /><table border="1"> <tr> <td><font color="white">Made by <font color="blue">Liam Monks</font></font></td> </tr> </table> </font> </body> </html> AND Addition.php <html> <head> <title>Maths Addition</title> </head> <body bgcolor="black"> <font color="white"> <center><h1>Addition Form</h1> <?php echo "The answer to your addition is..."; echo ($x + $y); ?> <br /><br /><br /><br /><br /><table border="1"> <tr> <td><font color="white">Made by <font color="blue">Liam Monks</font></font></td> </tr> </table> </font> </body> </html> Thanks, i can't find the error Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 You are assuming register_globals is on, which is very bad and is a huge security risk. Good thing they are not on (thus the zero). Try this: <html> <head> <title>Maths Addition</title> </head> <body bgcolor="black"> <font color="white"> <center><h1>Addition Form</h1> <?php $x = isset($_GET['x'])?$_GET['x']:0; $y = isset($_GET['y'])?$_GET['y']:0; echo "The answer to your addition is..."; echo ($x + $y); ?> <br /><br /><br /><br /><br /><table border="1"> <tr> <td><font color="white">Made by <font color="blue">Liam Monks</font></font></td> </tr> </table> </font> </body> </html> See if that gets you an answer. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 Im new too php. So im not sure what the code you put in does, so good you please explain for the future, thanks Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 <?php // if $_POST['x'] has been set, assign $x the value inside of $_POST['x'] else assign it zero. // this is called the ternary operator (? which is really a shortened version of // the if/else. $x = isset($_POST['x'])?$_POST['x']:0; $y = isset($_POST['y'])?$_POST['y']:0; echo "The answer to your addition is..."; echo ($x + $y); ?> A side note, it should have been $_POST not $_GET as we are posting the data from the form and not getting. Hope that helps for more information google php ternary operator Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 Thanks, I noticed it should of been POST i was like what the hell... LOL but anyway, here is the result: http://www.liamproductions.com/maths.php Quote Link to comment Share on other sites More sharing options...
rcorlew Posted July 6, 2007 Share Posted July 6, 2007 one problem, though it may be small, if you use really large number like 123455421526623616546542145 and try adding 215454842623656166532165 it gives a really odd answer like 1.3684765454894565E+17 Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 well thats the answer Its using the normal PHP addition. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 I need more help... How can i make it so only 10 Digits can be entered into each box... and so only numbers can be entered? Quote Link to comment Share on other sites More sharing options...
yzerman Posted July 6, 2007 Share Posted July 6, 2007 Thanks, I noticed it should of been POST i was like what the hell... LOL but anyway, here is the result: http://www.liamproductions.com/maths.php using POST or GET is fine, but both of those reside in $_REQUEST - I would get into the habit of using $_REQUEST for submitted data instead of using either $_POST or $_GET, it makes the code cleaner and easier to read and understand. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 one problem, though it may be small, if you use really large number like 123455421526623616546542145 and try adding 215454842623656166532165 it gives a really odd answer like 1.3684765454894565E+17 Really odd answer? You do know that is a valid answer called Scientific notation right? But yes if you using the standard math functions in PHP that is what you get, there is an extension, not sure what it is called, that can be installed to avoid that. Reply to Yzerman, $_REQUEST works, but how I see it is, I want to know that I am getting my data how I intended. Using the $_REQUEST, someone just has to append that data on to the URL and it will still be read, could be a potential security risk. That is why I stick to $_GET and $_POST. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 So everytime i send data to another page i have to use... $var = isset($_POST[''name"])?$_POST['name']:0; echo $var to get it on the page? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 Only on pages you are posting data to. You do not "HAVE" to do that, but that will avoid errors and make the script run more efficient. Plus provides a layer of security for you. Just good coding practice. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 oh ok, but won't this make the pages more longer than normal? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 oh ok, but won't this make the pages more longer than normal? No, not really I guess. I may add a few extra lines for variable declaration, but that should be done anyways. Defining variables, although unnecessary in PHP is good practice to prevent unknown data from entering into the page which could be potentially harmful. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 Yeah but if you have register globals on... you don't have to type all that out do you? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 Yeah but if you have register globals on... you don't have to type all that out do you? You do not want register_globals on. Type all that out ? it really takes just a few seconds. Anyhow if you want security, yes that is the best and securest way to do it. If you want to be left open, turn register_globals on. See how long it will take for a script kiddie to trash your site. Fun stuff. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 Oh ok, Thanks for the suggestion, But if i make a quiz with like 50 questions, it will take me a longer time to write out those 50 lines than when its on won't it? but i guess its worth it. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 Thats when you get creative with the code. <?php for ($i=0; $i<50; $i++) { $questions[$i] = isset($_POST['question'.$i]):$_POST['question'.$i]:''; } echo 'First Question: ' . $questions[1]; ?> Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 Ahh, so you would use arrays to do something big like that? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 Generally speaking, yes. Arrays are very nice because they are easily looped through, where as just a static variable, well you cannot really loop it etc. You will find arrays have many uses, and make life much easier for the most part. Especially with x-dimensional arrays =) Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 Ok thanks, Im new to PHP so i don't know much. Quote Link to comment Share on other sites More sharing options...
JP128 Posted July 6, 2007 Share Posted July 6, 2007 Yes, It was a very long time before I fully understood what arrays could be used for, and actually, I still don't know all about them. Like you, Liam, I am a little new to PHP, and finally got an ad-free server that runs Perl, PHP, MySQL, and a few others all for free. But if you are just testing, and don't want to do editing via FTP, if you do it that way, you can get WAMP(Windows Apache MySQL PHP) which is a free server that runs on your computer. http://www.wampserver.com/. But good luck on learning Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 Thanks JP, I just installed Wamp Server about a hour ago. Its ok. Its loops that i mainly don't understand but i guess i will if i keep looking at loop tutorials... jEdit helps seen as i have changed the colors to suit me... I like the black background i set because i feel like a coder then Quote Link to comment Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 Thanks JP, I just installed Wamp Server about a hour ago. Its ok. Its loops that i mainly don't understand but i guess i will if i keep looking at loop tutorials... jEdit helps seen as i have changed the colors to suit me... I like the black background i set because i feel like a coder then black background? That would kill my eyes, I tried that a long time ago, never stuck. My favorite program is Notepad++ for small projects and see my sig for eclispe for bigger projects. Loops are actually pretty easy, but most people struggle with them. I remember a time when I used to get infinite loops, but now it just never happens. Anyhow best of luck in your coding endeavors, remember to post here for help. Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 6, 2007 Author Share Posted July 6, 2007 Thanks... I like the black background i've put the writing light though the black background makes me feel like a Coder I guess the topic is solved. 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.