If (isset($_POST['submit']) Not Working Correctly

Hi,

I have the following code that I want to run only when the “Submit” button is pressed…but a message is echoed even when it has not been clicked…I can’t work out what I need to amend to make it work? :rolleyes:

<?php

if (isset($_POST['submit']) && (verify_user ('usernames.txt',$_POST['username'],$_POST['passwd']) == 1))
		{
		echo "User name matched!&lt;br /&gt;";

		}else{
		
		echo "username does not exist";

		}
						
?&gt;

you say that when nothing has been submitted to echo out that the username doesn’t exist;

try this:



<?php


if (isset($_POST['submit']) {

    if (verify_user ('usernames.txt',$_POST['username'],$_POST['passwd']) == 1)) {
        echo "User name matched!<br />";
    }else{
        echo "username does not exist";
    }
}

?>

Wouldn’t it make more sense to check if the request is actually a POST requestion and a username/password has been submitted rather than the button name? Back to the problem (“a message is echoed even when it has not been clicked”) at hand, of course a message is echoed because you have an if and an else which takes care of the two available scenarios (the condition is true, or false). Do you mean to say that User name matched!<br /> is always echoed?

There is seldom a need to give the submit button on a form a name and where there is you should never name it submit as that can overwrite the actual submit processing in some browsers with a reference to the button making it impossible to actually submit the form.

Thanks for your reply fristi…when I run the code I get the following error…

Parse error: syntax error, unexpected ‘{’ in /home/test/folder1/LogOnPage.php on line 60

I forgot a closing ):


<?php

if (isset($_POST['submit'])) {


    if (verify_user ('usernames.txt',$_POST['username'],$_POST['passwd']) == 1)) {

        echo "User name matched!<br />";

    }else{

        echo "username does not exist";

    }

}
?>

You can study this code to see what the difference is with yours so that you can learn :slight_smile: I used to code with a $_POST[‘submit’], but like the other others have mentioned in this topic, it is better to use the following code, no mentioning of a submit button. You just check if the $_POST array contains any values. If it does, something has been submitted. If something has submitted, you can first check if the values you need have been entered…

Just an example on how you can do things :slight_smile:


<?php

if (!empty($_POST)) {

    if (empty($_POST['username']) || empty($_POST['passwd'])) {

        echo 'please fill in both the user name and password';

    } else {

        if (verify_user('usernames.txt',$_POST['username'],$_POST['passwd']) == 1)) {

            echo "User name matched!<br />";

        }else{

            echo "username does not exist";
        }
    }
}
?>

Thank you all - I have managed to get it working from your help :O)