Page 1 of 1
Form input counter
Posted: Sat Aug 02, 2014 7:22 am
by wendyj
I am doing an exercise called "i'm thinking of a number" and part of the script requires you print how many turns it has been. I cannot get the turns thing right - everything else is working. Can someone take a look at my code and tell me what i am doing wrong.
Code: Select all
<?php
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($guess == ""){
print "Welcome!";
$random = rand(1,5);
print "$random";
?>
<form method="post">
<input type="text" name="guess" value="">
<input type="hidden" name="random" value="<?php echo $random ?>">
<input type="submit">
<?php
} else if($guess == $random){
print "$guess is Correct!";
$random == $tryAgain;
} else {
if($guess > $random){
print "Too High <br />";
} else if($guess < $random){
print "Too Low <br />";
}
print "Try Again!";
?>
<form method="post">
<input type="text" name="guess" value="">
<input type="hidden" name="random" value="<?php echo $random ?>" >
<input type="hidden" name="count" value="<?php echo $count ?>" >
<input type="submit">
</form>
<?php
$count = 0;
$count++;
print "<h3>$count</h3>";
}
?>
Re: Form input counter
Posted: Sat Aug 02, 2014 7:33 am
by wendyj
I just figured it out myself by putting this code in: $count++; print "<h3>$count</h3>";
Code: Select all
...
print "Try Again!";
$count++;
print "<h3>$count</h3>";
?>
<form method="post">
<input type="text" name="guess" value="">
<input type="hidden" name="random" value="<?php echo $random ?>" >
<input type="hidden" name="count" value="<?php echo $count ?>" >
<input type="submit">
</form> ...
Re: Form input counter
Posted: Sat Aug 02, 2014 9:36 am
by Christopher
A simpler way to think about this is to first deal with $count. If it is passed then increment it. If it is not passed then initialize it.
Code: Select all
if(isset($_POST['count'])){
$count = intval($_POST['count']) + 1;
} else {
$count = 0;
}
Then display the value.
Code: Select all
<input type="hidden" name="count" value="<?php echo $count ?>" >
The problems with the code above was that you were that you were 1) displaying $count before incrementing it and 2) setting it to 0 every time.
Re: Form input counter
Posted: Tue Aug 05, 2014 1:33 am
by wendyj
Thank you Christopher.
Re: Form input counter
Posted: Tue Aug 05, 2014 8:33 pm
by chris93
hi guys, i did a slight modification of this code.
once i enter some values for guess in the first field which is equal to $random , ie $guess = 1 , the program would go into the else part and print try again without getting the values for $random and $tryAgain.
and once i reenter some values again for guess , the program will go into the the if($guess == "") part and print print "Welcome!" and "$random.
how come the program treat $guess as an empty string ie $guess == "", even though i have entered some values for it ?
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']; }
$count = 0;
$count++;
print "<h3>$count</h3>";
?>
<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">
<?php
if($guess == "")
{
print "Welcome!";
$random = rand(1,5);
print "$random";
}
else if($guess == $random){
print "$guess is Correct!";
$random == $tryAgain;
} else {
if($guess > $random){
print "Too High <br />";
} else if($guess < $random){
print "Too Low <br />";
}
print "Try Again!";
?>
<form method="post">
<input type="text" name="guess" value="">
<input type="hidden" name="random" value="<?php echo $random ?>" >
<input type="hidden" name="count" value="<?php echo $count ?>" >
<input type="text" name="ab" value="ab" >
<input type="submit">
</form>
<?php
$count = 0;
$count++;
print "<h3>$count</h3>";
// this is all part of the else block
}
?>
Re: Form input counter
Posted: Tue Aug 05, 2014 8:58 pm
by chris93
ok, i have modified the code again ,
now once the user hits submit query, it will get the values for $guess and $count but still not for $random.
<?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']; }
$count = 0;
$count++;
print "<h3>$count</h3>";
?>
<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">
<?php
if($guess == "")
{
print "Welcome!";
$random = rand(1,5);
print "$random";
}
else if($guess == $random){
print "$guess is Correct!";
$random == $tryAgain;
} else {
if($guess > $random){
print "Too High <br />";
} else if($guess < $random){
print "Too Low <br />";
}
print "Try Again!";
?>
<form method="post">
<input type="text" name="guess" value="<?php echo $guess ?>" >
<input type="text" name="random" value="<?php echo $random ?>" >
<input type="text" name="count" value="<?php echo $count ?>" >
<input type="text" name="ab" value="ab" >
<input type="submit">
</form>
<?php
$count = 0;
$count++;
print "<h3>$count</h3>";
// this is all part of the else block
}
?>
Re: Form input counter
Posted: Tue Aug 05, 2014 9:28 pm
by Celauran
Your code is a little all over the place here. You've got two forms, one of which isn't closed. You're setting $count to whatever was sent back by the form (ie. $_POST['count']), then immediately overwriting that by setting $count to 0, and then immediately incrementing it to 1. You're then setting it back to 0 and incrementing again farther down in the code. Might make more sense to initialize your variables to some default values, then check if the form has been submitted and update those values accordingly, then, last, evaluate the submitted guess.
Re: Form input counter
Posted: Wed Aug 06, 2014 2:17 am
by wendyj
Hi Christopher,
I really like your piece of code you gave me:
Code: Select all
if(isset($_POST['count'])){
$count = intval($_POST['count']) + 1;
} else {
$count = 0;
Two things though - At the beginning of my document I have this code:
Code: Select all
if(isset($_POST['count'])){ $count = $_POST['count']; }
I presume that the 'isset' in your code does the same job?
And, I have looked up 'intval' - and it seems to be a function that changes a number to an integer if it has a decimal place - is this correct?
Re: Form input counter
Posted: Wed Aug 06, 2014 6:50 am
by Celauran
wendyj wrote:Two things though - At the beginning of my document I have this code:
Code: Select all
if(isset($_POST['count'])){ $count = $_POST['count']; }
I presume that the 'isset' in your code does the same job?
The difference is really in the else clause. Your $count value is left uninitialized if $_POST['count'] is not set, which will generate an undefined variable notice when you try to use it in your form. Christopher's code initializes it to 0 if it wasn't already set by the form.
Re: Form input counter
Posted: Wed Aug 06, 2014 3:36 pm
by Christopher
wendyj wrote:And, I have looked up 'intval' - and it seems to be a function that changes a number to an integer if it has a decimal place - is this correct?
intval() will convert any type of variable to an integer. This script expects an integer, so make sure it gets it. It is a good habit to validate and filter every input that comes from the user (or the web server). You want to make sure that someone cannot pass a value in count that is not an integer. In this case it many not cause a problem, but there clever ways to inject malicious things into your scripts though POST/GET variables.