Reputation: 3163
Here is a code that I've made:
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['name'])){
$sum+=1;
}
echo "sum = $sum";
?>
When I enter some text in the form and click Validate, the page display sum=1
, but after this, when I enter nothing in the form and click Validate, the page STILL displays sum=1
.
Why does the variable $sum is not reloaded between the two Validate ? Is there a way to escape it ?
Thanks
Upvotes: 0
Views: 65
Reputation: 2504
Another way would be to check the request that your client has made on your page. So that if it is a simple refresh (not with a form refresh), it is a GET request and so, the variable should not be incremented and if the form has been sent, then you can do whatever you want such as incrementing the data.
So if the client is sending the form with an input text filled, then you can increment the value. In all other cases, the value should remain a zero.
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['name']) && !empty($_POST['name']))
{
$sum++; /* strictly equivalent to: $sum += 1; */
}
?>
<samp>sum = <?php echo $sum; ?></samp>
Upvotes: 0
Reputation: 630
This is because isset()
checks for the existence of the $_POST variable. In your case, the $_POST variable exists and has an empty string value.
Your code will work if you change isset()
to !empty()
like so;
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(!empty($_POST['name'])){
$sum+=1;
}
echo "sum = $sum";
?>
More about the empty()
function here.
Upvotes: 1
Reputation: 27
You have the code appending 1 to variable $sum but your if statement is based on the name field being passed. Not if the name field has any data in it. So... you have made your code add 1 as long as name field is passed, regardless if it has text input or not.
Also, you should reassign the varible to reset it. += should just be =
<form method="post" action="test.php">
//----------------------------- add empty value to input ------------
<input type="text" name="name" value="" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['name'])){
$sum=1;
}
echo "sum = $sum";
?>
Upvotes: 0
Reputation: 710
This will solve the issue
<?php
$sum=0;
if(isset($_POST['name']) && $_POST['name'] != ''){
$sum+=1;
}
echo "sum = $sum";
?>
Upvotes: 1
Reputation: 239
You can try below:
if(isset($_POST['name']) && strlen($_POST['name'])>0){
$sum+=1;
Upvotes: 0
Reputation: 1300
Try this
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" name="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['submit'])){
$sum+=1;
}
echo "sum = $sum";
?>
Upvotes: 0