Page 1 of 1

how to issue a postback after a condition is met

Posted: Wed Aug 06, 2014 9:31 am
by chris93
is it possible in php to issue a postback after a condition is met

for ex. i have a form which asks the user to guess a randomly generated number.

how do i make it that once the user enters guesses the random number correctly ie that is after

the condition if($guess==$random) is met ,

that the postback will reset the values for $count and $tryAgain to zero ?

Code: Select all




<?php

// gets the values from the user entered field 
// the first time the page loads
// guess and $tryAgain is not set 

if(isset($_POST['random'])){ $random = $_POST['random']; }
if(isset($_POST['guess'])){ $guess = $_POST['guess']; }
if(isset($_POST['tryAgain'])){ $tryAgain = $_POST['tryAgain']; }
if(isset($_POST['count'])){ $count = $_POST['count']; }

if(isset($_POST['random']))

// after postback get value for random from the post request 
{ $random = $_POST['random'];}
 else 
 // first time form loads, generate random number ,  if value has not been set set it 
 {$random =  rand(1,5); }


 
 
 if(isset($_POST['count']))

// after postback increment count by 1 
{ $count = $_POST['count']+1; }
 else 
 // if value has not been set set it 
 {$count = 0 ; }
 
 
 if(isset($_POST['count']))

// after postback increment count by 1 
{ 
  if($guess==$random)
     { $count = 0; 
	   $tryAgain =0 ; }
	   }
 else 
 // if value has not been set set it 
 {$count = 0 ; }
 
 
 
 
 


 
		
if(isset($_POST['tryAgain']))

// if postrequest get value for random from the post request 
//           the previous value of count + 1 
{ $tryAgain = $_POST['count'] + 1 ;}
 else 
 // if value has not been set set it 
 {$tryAgain =  $count; }		
		
		?>
		
<form method="post">
<input type="text" name="guess" value="">
<input type="text" name="random" value="<?php echo $random ?>">\

<input type="text" name="tryAgain" value="<?php echo $tryAgain ?>">
<input type="text" name="count" value="<?php echo $count ?>" >


<input type="submit">


<form method="post"  >
<
<input type="submit">




		<?php
if($guess == "")

{
       
        print "Welcome!";
        
       

        print "$random";
        
		
}

 else if($guess == $random){
        print "$guess is Correct!";
        $random == $tryAgain;

// issue postback here reset $count and $tryAgain to 0
		$tryAgain = 0;
                $count = 0 ;

		
       
} else {
        if($guess > $random){
                print "Too High <br />";
        } else if($guess < $random){
                print "Too Low <br />";
        }
               
       
        print "Try Again!";
        ?>
   

   
   
   // insert
   
<?php

        $count = 0;
        $count++;
        print "<h3>$count</h3>";

   // this is all part of the else block     
}


?>



Re: how to issue a postback after a condition is met

Posted: Wed Aug 06, 2014 3:41 pm
by Christopher
What is not working in the code you posted?

Re: how to issue a postback after a condition is met

Posted: Fri Aug 08, 2014 11:05 am
by chris93
what im trying to do is that once the user guesses the random number correctly , ie $random = 2 , the user enters the correct random number 2 for $guess and clicks the submit button, $count and $tryAgain will be reset to zero.

Re: how to issue a postback after a condition is met

Posted: Fri Aug 08, 2014 6:22 pm
by Christopher
Just move the HTML that generates the form to after all you logic. Then the form should be filled in with the correct values. It also makes a cleaner separation between the domain logic code and the presentation output generation code.

PS - Your formatting makes it very difficult to read your code, so I can really tell what it is doing. I would recommend generally following PHP formatting standards: http://www.php-fig.org/psr/psr-2/

Code: Select all

<?php

// gets the values from the user entered field 
// the first time the page loads
// guess and $tryAgain is not set 

if(isset($_POST['random'])){ $random = $_POST['random']; }
if(isset($_POST['guess'])){ $guess = $_POST['guess']; }
if(isset($_POST['tryAgain'])){ $tryAgain = $_POST['tryAgain']; }
if(isset($_POST['count'])){ $count = $_POST['count']; }

if(isset($_POST['random']))

// after postback get value for random from the post request 
{ $random = $_POST['random'];}
 else 
 // first time form loads, generate random number ,  if value has not been set set it 
 {$random =  rand(1,5); }

 if(isset($_POST['count']))

// after postback increment count by 1 
{ $count = $_POST['count']+1; }
 else 
 // if value has not been set set it 
 {$count = 0 ; }
 
 
 if(isset($_POST['count']))

// after postback increment count by 1 
{ 
  if($guess==$random)
     { $count = 0; 
	   $tryAgain =0 ; }
	   }
 else 
 // if value has not been set set it 
 {$count = 0 ; }
 		
if(isset($_POST['tryAgain']))

// if postrequest get value for random from the post request 
//           the previous value of count + 1 
{ $tryAgain = $_POST['count'] + 1 ;}
 else 
 // if value has not been set set it 
 {$tryAgain =  $count; }		
		
if($guess == "")

{
       
        print "Welcome!";
        
       

        print "$random";
        
		
}

 else if($guess == $random){
        print "$guess is Correct!";
        $random == $tryAgain;

// issue postback here reset $count and $tryAgain to 0
		$tryAgain = 0;
                $count = 0 ;

		
       
} else {
        if($guess > $random){
                print "Too High <br />";
        } else if($guess < $random){
                print "Too Low <br />";
        }
               
       
        print "Try Again!";
 
        $count = 0;
        $count++;
        print "<h3>$count</h3>";

   // this is all part of the else block     
}


?>
		
<form method="post">
<input type="text" name="guess" value="">
<input type="text" name="random" value="<?php echo $random ?>">\

<input type="text" name="tryAgain" value="<?php echo $tryAgain ?>">
<input type="text" name="count" value="<?php echo $count ?>" >

<input type="submit">
</form>