Page 1 of 1

Dice roll problem

Posted: Tue Oct 04, 2011 7:36 pm
by quintin1
Hi guys, first post here
Looks like im going to be here alot, since im taking a PHP course and im quite lost.
Anyways, im having trouble with this code. I think its the way i assigned my variables. Basically i need to make a program that he user inputs a number from 1-6 and a die is rolled and an image is displayed. If the user input the correct number shown on the die, they are awarded one point (this is to be done with session variables which i havent a clue how to do them correctly.
Anything will help.

Heres the form:
<body>

<form name = "n" action="diceroll.php" method="post">
Enter your roll: <br>
<input type= "text" name="1">

<input type ="submit" name = "submit">

</form>
</body>

and the PHP:

Code: Select all



<body>
<?php
$WIN = $_POST['guess'];
$dice =	 rand(1, 6);
    if ($WIN == $dice){ 
	echo "You Guessed Correctly!";
	}else {
	     echo "You have Guesed Incorrectley". "<br>";
		 }
          
	 
	if($dice == 1) echo "<img src='dice1.png'>"."<br>";
	if($dice == 2) echo "<img src='dice2.png'>"."<br>";
	if($dice == 3) echo "<img src='dice3.png'>"."<br>";
	if($dice == 4) echo "<img src='dice4.png'>"."<br>";
	if($dice == 5) echo "<img src='dice5.png'>"."<br>";
	if($dice == 6) echo "<img src='dice6.png'>"."<br>";

//else {
	     //echo "You have Guesed Incorrectley". "<br>";
		 //}
		 
?>

<form name= "back" action= "diceroll.html" method= "post">
<input type="submit" name="back" value ="Play Again">
</form>
</body>

Re: Dice roll problem

Posted: Tue Oct 04, 2011 7:53 pm
by Celauran
Your form has

Code: Select all

<input type= "text" name="1">
but you're trying to process

Code: Select all

$WIN = $_POST['guess'];
They need to match.

Re: Dice roll problem

Posted: Tue Oct 04, 2011 8:11 pm
by quintin1
i fixed it but now the else statement isnt working, do i need to set a condition for it?

Re: Dice roll problem

Posted: Tue Oct 04, 2011 8:14 pm
by quintin1
scratch that, now im stuck on the session variable.

Re: Dice roll problem

Posted: Tue Oct 04, 2011 8:23 pm
by Celauran
quintin1 wrote:scratch that, now im stuck on the session variable.
Can you elaborate? What are you doing? What isn't working?

Re: Dice roll problem

Posted: Tue Oct 04, 2011 9:25 pm
by quintin1
im trying to store the amount of times that the user correctly inputs the variable, which is guessing the dice roll correctly so that it stores the data like a cookie.

Re: Dice roll problem

Posted: Wed Oct 05, 2011 6:56 am
by Celauran
Yes, I got that, but how are you going about it. We can't tell you what's wrong with code we can't see.