Form input counter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
wendyj
Forum Newbie
Posts: 12
Joined: Fri Aug 01, 2014 3:14 am
Location: South Africa

Form input counter

Post 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>";

	
}


?>
User avatar
wendyj
Forum Newbie
Posts: 12
Joined: Fri Aug 01, 2014 3:14 am
Location: South Africa

Re: Form input counter

Post 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> ...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Form input counter

Post 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.
(#10850)
User avatar
wendyj
Forum Newbie
Posts: 12
Joined: Fri Aug 01, 2014 3:14 am
Location: South Africa

Re: Form input counter

Post by wendyj »

Thank you Christopher.
chris93
Forum Newbie
Posts: 10
Joined: Fri Aug 01, 2014 4:43 pm

Re: Form input counter

Post 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     
}


?>













chris93
Forum Newbie
Posts: 10
Joined: Fri Aug 01, 2014 4:43 pm

Re: Form input counter

Post 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
}


?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form input counter

Post 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.
User avatar
wendyj
Forum Newbie
Posts: 12
Joined: Fri Aug 01, 2014 3:14 am
Location: South Africa

Re: Form input counter

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Form input counter

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Form input counter

Post 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.
(#10850)
Post Reply