how to issue a postback after a condition is met
Posted: Wed Aug 06, 2014 9:31 am
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 ?
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
}
?>