Forum Moderators: coopster

Message Too Old, No Replies

Beginner Q - Forms & PHP

Using a variable

         

wkpride

12:09 am on Dec 11, 2007 (gmt 0)

10+ Year Member



Hey,

Probably an easy question, but darned if I can make it work!

I want to make a random number (1-10) added to another random number (1-10), the user will use a form to fill out the answer.

Output would look something like this:

6 + 8 = [_______] <--- that "box" is "text input". The six and 8 would be variables.

When I write the code for it, I can't insert a variable in front of the <input type="text"....>

Here is a little of the code:
<?php
$r1 = rand(1,10);
$r2 = rand(1,10);
$answer=($r1=$r2);
?>
<form action="1random.php" method="POST">
<input type="submit" value="Send Answer" />
</form>

It seems so simple, but I'm scratching my head...

Thanks,

KP

eelixduppy

12:29 am on Dec 11, 2007 (gmt 0)



Something like this?

<?php
$r1 = rand(1,10);
$r2 = rand(1,10);
$answer=($r1+$r2);
?>
<form action="1random.php" method="POST">
<?php
echo $r1." + ". $r2;
?>
<input type="text" name="answer" />
<input type="submit" value="Send Answer" />
</form>

wkpride

1:04 am on Dec 11, 2007 (gmt 0)

10+ Year Member



Thanks for quick reply!

I didn't realize you could split the <form> lines apart. Excellent!

I have it almost... It looks like I should be setting variables to 0 before I accept input?

Not sure why, but it always prints "Sorry"....?

<?php
echo $r1." + ". $r2;
?>
<input type="text" name="answer" />
<input type="submit" value="Send Answer" />
</form>
<?php
echo $answer;
$a = $_REQUEST['answer'];
echo $a;
if ($answer=$a)
{
echo "Great!";
}
echo "Sorry";

phranque

1:20 am on Dec 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



but it always prints "Sorry"....?

as it should!

you could try:
if ($answer=$a)
{
echo "Great!";
}else{
echo "Sorry";
}

wkpride

1:26 am on Dec 11, 2007 (gmt 0)

10+ Year Member



Hhaha. What ELSE could it have been?

If it'd been a snake, it'd bitten me!

Thanks a bunch.

....KP

coopster

5:51 pm on Dec 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if ($answer=$a)

This is not a test of equality yet though, just so you know! Use the double equal sign. You'll want to read through assignment [php.net] and comparison [php.net] to understand the difference.

phranque

6:44 pm on Dec 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



This is not a test of equality yet though, just so you know! Use the double equal sign.

my bad, coop!
i meant to fix that when i copied kp's sample code...

wkpride

3:02 am on Dec 12, 2007 (gmt 0)

10+ Year Member



Thanks everyone.

I was working on a small routine earlier & was using"

if ($a=$b)
do my routine
else
quit

I think $a & $b were strings.

Anyhow, they were always equal. I finally figured it must have been assigning $b value into $a. Which, i think, always made the statement true. That took a while to figure.

So what's the difference between == and ===. I believe I used === on the routine today (and it worked).

Thanks!

kp

jatar_k

3:13 pm on Dec 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



[php.net...]