Jump to content

Help with php math game


laquerhead

Recommended Posts

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

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>";

?>

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>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.