Page 1 of 1

Simple math with PHP

Posted: Mon Nov 01, 2010 12:03 pm
by asai
Hi

I have a daughter struggling with simple math at school, and I want to make a web page that she can practice.
It should start with the simplest tasks like addition and subtraction.

I tried this code:

Code: Select all

<?php
 
$a = rand(0, 10); 
$b = rand(0, 10);

$answer = ($b+$a);

echo $a + $b;

<form> 
Svar: <input type="text" />
<input type="submit" />
</form>

if ( "text" == $answer ) {
	echo "The answer is correct";
} else {
	echo "The answer is wrong";
}

?>
But this don't work. :(

Re: Simple math with PHP

Posted: Mon Nov 01, 2010 1:26 pm
by AbraCadaver
You need to name the input and then access it with GET or POST vars. You should also check to see that the form was posted so you don't get notices. Also, you probably don't want to echo the answer:

Code: Select all

$a = rand(0, 10);
$b = rand(0, 10);

$answer = $a + $b;

echo $a . ' + ' . $b;

<form method="POST">
Svar: <input type="text" name="answer"/>
<input type="submit" name="submit" />
</form>

if ( isset($_POST['submit'] && $_POST['name'] == $answer ) {
        echo "The answer is correct";
} else {
        echo "The answer is wrong";
}

Re: Simple math with PHP

Posted: Mon Nov 01, 2010 3:33 pm
by asai
This code works:

Code: Select all

<?php
 
$a = rand(0, 10);
$b = rand(0, 10);

$answer = $a + $b;

echo $a . ' + ' . $b;

echo $answer;

?>
However, when I try to put in a input field to give the anwer, the page output is blank.
This code doesn't work:

Code: Select all

<?php
 
$a = rand(0, 10);
$b = rand(0, 10);

$answer = $a + $b;

echo $a . ' + ' . $b;

<form method="POST">
<input type="text" name="answer"/>
<input type="submit" name="submit" />
</form>

if ( isset($_POST['submit'] && $_POST['name'] == $answer ) {
        echo "The answer is correct";
} else {
        echo "The answer is wrong";
}

?>

Re: Simple math with PHP

Posted: Tue Dec 21, 2010 10:46 am
by asai
I want to give this code a new try...
Any suggestions on what I could try to make this work?
My daughter need to work on her math skills over the holidays... :)

EDIT: The edited script...

Code: Select all

<?php
// math.php

echo <<<_END

$a = rand(0, 10);
$b = rand(0, 10);

$answer = $a + $b;

echo $a . ' + ' . $b;

<form method="post" action="math.php">
  Answer:	<input type="text" name="a" /><br />
  <input type="submit" value="Check">
</form>

_END;

if ( isset($_POST['submit'] && $_POST['a'] == $answer ) {
        echo "The answer is correct";
} else {
        echo "The answer is wrong";
}

?>
But the page is blank... :(

Re: Simple math with PHP

Posted: Tue Dec 21, 2010 10:56 am
by mikosiko
Just a suggestion.... if this is not a project for you and you main goal is provide your daughter with good tools why to waste time trying to re-invent the wheel yourself? ... is a lot of good and free product in the market that she (you) can use immediately ... maybe your should consider alternatives like :
http://edubuntu.org/other-educational-systems

all of this alternatives provide a live CD that you can use in your computer without install anything... simple... quick and more efficient for your goal than develop something from scratch in with limited knowledge IMHO.

Re: Simple math with PHP

Posted: Tue Dec 21, 2010 11:01 am
by asai
@mikosiko
I agree with you, however she is very young, so my thought is to make this veeery simple. At first, but later when she gets better, I can try something more advanced. :)