laquerhead Posted June 2, 2009 Share Posted June 2, 2009 Hi, I'm new to PHP and I'm trying to create a simple math game which utilizes an html form to pass on the user's answer to a php script that checks that answer. Here is what I have thus far: game.php: <!-DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-TRansitional.dTD"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>PHP Practice</title> </head> <body> <?php function getRand(){ return rand(1,13); } $lhs=getRand(); $rhs=getRand(); ?> <form action="welcome.php?lhs=$lhs&rhs=$rhs" method="get"> <label value="<?php $lhs;?>" /> <label value="<?php $rhs;?>" /> <input type="text" name="answer" /> <input type="submit" value="Check Answer" /> </form> </body> </html> check.php <?php $lhs = $_GET['lhs']; $rhs = $_GET['rhs']; $correct_answer = $lhs * $rhs; if( $_GET['answer'] == $correct_answer ) echo "Correct!"; else echo "Incorrect. " . $lhs . " X " . $rhs . " = " . $correct_answer; ?> Of course it's not working. I know I'm probably way off and that the value="<?php ... thing won't work like I'm used to in asp pages. Your help/guidance is most apprectiated! Thank You in advance Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/ Share on other sites More sharing options...
Alex Posted June 2, 2009 Share Posted June 2, 2009 echo the variable within the value. Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/#findComment-848115 Share on other sites More sharing options...
Maq Posted June 2, 2009 Share Posted June 2, 2009 - Please surround your code with tags. - You need to echo out the var. (the form action should be fine) i.e. - Also, what exactly doesn't work? What happens? Errors? Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/#findComment-848118 Share on other sites More sharing options...
ldougherty Posted June 2, 2009 Share Posted June 2, 2009 This should work.. <?php function getRand(){ return rand(1,13); } $lhs=getRand(); $rhs=getRand(); echo "<form action='check.php' method='GET'>"; echo "<input type='hidden' name='lhs' value='$lhs'>"; echo "<input type='hidden' name='rhs' value='$rhs'>"; echo "$lhs * $rhs = "; echo "<input type='text' size='5' name='answer'>"; echo "<input type='submit' value='Check Answer'>"; echo "</form>"; ?> Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/#findComment-848121 Share on other sites More sharing options...
laquerhead Posted June 2, 2009 Author Share Posted June 2, 2009 Thank you kindly! That works perfectly. One question, is the "echo" method you used the preferred/standard way to accomplish this? I don't want to come off as being unappreciative, it just seems like a bit of a hack to me... Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/#findComment-848207 Share on other sites More sharing options...
Alex Posted June 2, 2009 Share Posted June 2, 2009 It's not a hack. It's the 'correct' way. 'echo' just means that it's outputting it to the browser. Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/#findComment-848208 Share on other sites More sharing options...
Ken2k7 Posted June 2, 2009 Share Posted June 2, 2009 If you don't like echo, then just do this - <?php function getRand(){ return rand(1,13); } $lhs=getRand(); $rhs=getRand(); ?> <form action='check.php' method='GET'> <input type='hidden' name='lhs' value='<?php echo $lhs; ?>'> <input type='hidden' name='rhs' value='<?php echo $rhs; ?>'> <?php echo "$lhs * $rhs = "; ?> <input type='text' size='5' name='answer'> <input type='submit' value='Check Answer'> </form> Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/#findComment-848211 Share on other sites More sharing options...
Alex Posted June 3, 2009 Share Posted June 3, 2009 If you don't like echo, then just do this - <?php function getRand(){ return rand(1,13); } $lhs=getRand(); $rhs=getRand(); ?> <form action='check.php' method='GET'> <input type='hidden' name='lhs' value='<?php echo $lhs; ?>'> <input type='hidden' name='rhs' value='<?php echo $rhs; ?>'> <?php echo "$lhs * $rhs = "; ?> <input type='text' size='5' name='answer'> <input type='submit' value='Check Answer'> </form> That's what he was doing originally, just forgetting the 'echo'. That's what I thought he meant. Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/#findComment-848214 Share on other sites More sharing options...
laquerhead Posted June 3, 2009 Author Share Posted June 3, 2009 It's not a hack. It's the 'correct' way. 'echo' just means that it's outputting it to the browser. Thank you all. Everything's working great! Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/#findComment-848227 Share on other sites More sharing options...
Maq Posted June 3, 2009 Share Posted June 3, 2009 It's all rendered as HTML by the browser in the end, it doesn't matter if you just put in raw HTML or echo it out. Link to comment https://forums.phpfreaks.com/topic/160705-help-with-php-math-game/#findComment-848533 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.