slannesh Posted June 26, 2012 Share Posted June 26, 2012 Hi. I am new to PHP and I'm trying to check if the passwords entered in are not validating properly. Basically its supposed to see if the user typed in the password correctly on both text boxes. I get no errors but when I submit info from one page to the other, even if the passwords match it still says that the "Password is not typed in correctly". submit.php <?php $username = $_POST['username']; $password = $_POST['password']; $re_password = $_POST['re_password']; $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; if ($username = '') { echo "Username is blank"; } elseif ($password = '') { echo "Password is blank"; } elseif ($password == $re_password) { echo "Password is not typed in correctly"; } elseif ($firstname = '') { echo "First name is blank"; } elseif ($lastname = '') { echo "Last name is blank"; } else echo "WOOT"; ?> new_user.html <head> <meta http-equiv="Content-Type" content="test/html; charset=utf-8" /> <title>Prototype_NewUser</title> <link href="./main.css" rel="stylesheet" type="text/css"></link> </head> <body> <form action='./submit.php' method='post'> <label for="username">User name:</label> <input type="text" id="username" name="username" /><br /> <label for="password">Password:</label> <input type="text" id="password" name="password" /><br /> <label for="re_password">Re-enter password:</label> <input type="text" id="re_password" name="re_password" /><br /> <label for="firstname">First Name:</label> <input type="text" id="firstname" name="firstname" /><br /> <label for="lastname">Last Name:</label> <input type="text" id="lastname" name="lastname" /><br /> <input type='submit' name='submit' value='submit'> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/264787-cant-seem-to-validate-passwords/ Share on other sites More sharing options...
kicken Posted June 26, 2012 Share Posted June 26, 2012 if ($username = '') { A single equal sign is an assignment. What your doing there is assigning an empty value to $username, rather than checking to see if it is empty. You need to use double-equals to test for a condition: if ($username == ''){ Quote Link to comment https://forums.phpfreaks.com/topic/264787-cant-seem-to-validate-passwords/#findComment-1357007 Share on other sites More sharing options...
cyberRobot Posted June 26, 2012 Share Posted June 26, 2012 <?php //... elseif ($password == $re_password) { echo "Password is not typed in correctly"; } //... ?> Assuming the above code is checking that the password matches the re-typed password field, you'll want to display an error if they don't match. To do that, you would use not equals: <?php //... elseif ($password != $re_password) { echo "Password is not typed in correctly"; } //... ?> Also, I would recommend checking out the trim() function: http://php.net/manual/en/function.trim.php Using trim() on the form input helps prevent errors from visitors accidentally typing a space before/after the username for example. <?php $username = trim($_POST['username']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/264787-cant-seem-to-validate-passwords/#findComment-1357061 Share on other sites More sharing options...
zPlus Posted June 26, 2012 Share Posted June 26, 2012 slannesh, before you start coding something I believe that reading the manual is still a good practice... Specifically to your code, you should check out how PHP operators work. I don't mean to be rude, but you should do your homework before asking for tips or help Quote Link to comment https://forums.phpfreaks.com/topic/264787-cant-seem-to-validate-passwords/#findComment-1357070 Share on other sites More sharing options...
slannesh Posted June 26, 2012 Author Share Posted June 26, 2012 Thanks for the help. I have been using a book. New to programing and what not. Quote Link to comment https://forums.phpfreaks.com/topic/264787-cant-seem-to-validate-passwords/#findComment-1357260 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.