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
Share on other sites

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

?>

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.