Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts

    Simple POST random number issue

    Ok I am trying to make something to ask you random multiplication questions. Now it asks the questions fine. Generates the random questions fine. But when it reloads the page the random numbers are different...

    how can I fix this?

    Code:
    <?php 
    
    $rndnum1 = rand(1, 12); 
    $rndnum2 = rand(1, 12);
    
    echo "<h3>". $rndnum1 . " x ";
    echo $rndnum2 . "</h3>";
    
    if($_SERVER["REQUEST_METHOD"] == "GET") {
    
    	$answer=0;
    
    }
    else if($_SERVER["REQUEST_METHOD"] == "POST") {
    
    	$answer=trim($_POST["answerInput"]);
    	$check=$rndnum1*$rndnum2;
    	
    	if($answer==$check) {
    	
    		echo "Correct!";
    	
    	}
    	else {
    	
    		echo "Wrong!";
    	
    	}
    	
    
    }
    
    ?>
    
    <form action="<?php echo $_SERVER['PHP_SELF'];  ?>" method="post" >
    	<table>
    		<tr>
    			<td>
    				First Name:&nbsp;
    			</td>
    			<td>
    				<input type="text" name="answerInput" value="<?php echo $answer; ?>" size="20"/>
    			</td>
    			<td>
    				<?php echo $answerError; ?>
    			</td>
    		</tr>
    		<tr>
    			<td class="signupTd" colspan="2">
    				<input type="submit" name="submit" value="Submit"/>
    			</td>
    		</tr>
    	</table>
    </form>
    Continually Learning

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Output the random number to a SESSION, which will be valid until the browser is closed.



    -------
    Leonard Whistler

  • #3
    Regular Coder MrEnder's Avatar
    Join Date
    Jul 2008
    Location
    Canada Ontario
    Posts
    258
    Thanks
    18
    Thanked 4 Times in 4 Posts
    Quote Originally Posted by Len Whistler View Post
    Output the random number to a SESSION, which will be valid until the browser is closed.



    -------
    how would I do that??? I'm not all that good at php. I was trying to make an easy application to help my kid study.
    Continually Learning

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Untested for syntax and logic. Might need some corrections.


    header.php
    PHP Code:
    <?php
    session_start
    ();
    if (!isset(
    $_SESSION['random_number'])) {
    $_SESSION['random_number'] = rand(1,9999);

    }
    $random_number $_SESSION['random_number'];
    ?>


    All other php pages - number should remain the same, until browser closed.
    PHP Code:
    <?php
    echo "$random_number"
    ?>




    -------
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    MrEnder (03-30-2010)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •