0

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

1
  • 1
    Because you are setting it hard to 0 a few lines before. The script is always executed complete and not starting with your if-statement Commented Dec 17, 2016 at 11:22

6 Answers 6

1

This will solve the issue

<?php
$sum=0;
if(isset($_POST['name']) && $_POST['name'] != ''){
    $sum+=1;
}
echo "sum = $sum";
?>
Sign up to request clarification or add additional context in comments.

3 Comments

I think it will not.
I have to revert myself. Understood the question wrong
Not an issue. It happens :p
1

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.

Comments

0

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>

Comments

0

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

Comments

0

You can try below:

if(isset($_POST['name']) && strlen($_POST['name'])>0){
$sum+=1;

Comments

0

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

    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.