eldan88 Posted November 21, 2013 Share Posted November 21, 2013 Hey guys. I have admin page that just says "Welcome. Please enter your username and password". I am trying to change that message if the username and password is empty or if it is not correct. Currently this is what I have .. $message = "<span style=\"font-size:14px;\"><strong>Welcome </strong></span>!<br> Please enter your username and password to log in"; if(isset($_POST['submit'])) { $errors = array(); $field_name = array('username','password'); if(empty($_POST[$field_name]) || !isset($_POST[$field_name])) { $errors[] = "Please enter a username or password"; } If they enter the wrong username and password I have the following if(mysql_num_rows($result) == 1) { //Redirect to Admin Page // Success } else { $errors[] = "Sorry your username or password is incorrect.<br />"; } And right above the username and password form I am displaying the following welcome message <div id="login_msg"> <?php if(!empty($message)) {echo $message."<br>" ;} foreach ($errors as $message) { echo $message; } Right now its just being appended to the actual $message. How can I completely change the text?? Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/284141-changing-error-message-for-form-validation/ Share on other sites More sharing options...
Ch0cu3r Posted November 21, 2013 Share Posted November 21, 2013 (edited) Only add Please enter your username and password to log in to $message when the form has not been submitted $message = "<span style=\"font-size:14px;\"><strong>Welcome </strong></span>!<br>"; if(isset($_POST['submit'])) { $errors = array(); $field_name = array('username','password'); if(empty($_POST[$field_name]) || !isset($_POST[$field_name])) { $errors[] = "Please enter a username or password"; } } else { $message .= "Please enter your username and password to log in"; // append this to $message (when form has not been submitted yet) } Edited November 21, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284141-changing-error-message-for-form-validation/#findComment-1459404 Share on other sites More sharing options...
eldan88 Posted November 21, 2013 Author Share Posted November 21, 2013 Chocu3. Thanks for the response, but it doesn't work :/. I made the "Welcome" into a string instead of assigning it to a variable $message. The message is not showing when there is an incorrect username/password. Below is the code I have changed If username and password is empty if(isset($_POST['submit'])) { $field_name = array('username','password'); if(empty($_POST[$field_name]) || !isset($_POST[$field_name])) { $error = "Please enter a username or password"; } If username password is incorrect if(mysql_num_rows($result) == 1) { // Redirect to admin section // Success } else { $error = "Sorry your username or password is incorrect.<br />"; } // Message to display <?php echo "<span style=\"font-size:14px;\"><strong>Welcome User</strong></span>!"."<br>"; if(!empty($message)) {echo $message."<br>" ;} if(!empty($error)) {echo $error."<br>" ;} ?> Quote Link to comment https://forums.phpfreaks.com/topic/284141-changing-error-message-for-form-validation/#findComment-1459412 Share on other sites More sharing options...
eldan88 Posted November 21, 2013 Author Share Posted November 21, 2013 Here is the whole form to make things easier if(isset($_POST['submit'])) { $field_name = array('username','password'); if(empty($_POST[$field_name]) || !isset($_POST[$field_name])) { $error = "Please enter a username or password"; } if(empty($error)) { $username = trim($_POST['username']); $password = trim($_POST['password']); $query = "SELECT username,password "; $query .= "FROM admin "; $query .= "WHERE username = '{$username}' "; $query .= "AND password = '{$password}' "; $query .= " LIMIT 1 "; $result = mysql_query($query); confirm_query($result); if(mysql_num_rows($result) == 1) { //Redirect // Success } else { $error = "Sorry your username or password is incorrect.<br />"; } } } else { $message = "Please enter your username and password to log in"; } ?> Output message <?php echo "<span style=\"font-size:14px;\"><strong>Welcome User</strong></span>!"."<br>"; if(!empty($message)) {echo $message."<br>" ;} if(!empty($error)) {echo $error."<br>" ;} ?> Quote Link to comment https://forums.phpfreaks.com/topic/284141-changing-error-message-for-form-validation/#findComment-1459413 Share on other sites More sharing options...
Joshua F Posted November 21, 2013 Share Posted November 21, 2013 (edited) I looked at your code and you have an array containing two field names and you try to throw that into a $_POST[], (if (empty($_POST[$field_name]) || !isset($_POST[$field_name])) {). This should be if (empty($_POST[$field_name[0]]) || !isset($_POST[$field_name[1]])) { should it not? Otherwise you're seeing if $_POST has a key inside of it with the key as array('username', 'password'). You should also define your variables if they even have a chance of not being defined before being called. if (isset($_POST['submit'])) { $error = ''; // set a blank error message $field_name = array('username', 'password'); if (empty($_POST[$field_name[0]]) || !isset($_POST[$field_name[1]])) { $error = "Please enter a username or password"; } if (empty($error)) { $username = trim($_POST['username']); $password = trim($_POST['password']); $query = "SELECT username,password "; $query. = "FROM admin "; $query. = "WHERE username = '{$username}' "; $query. = "AND password = '{$password}' "; $query. = " LIMIT 1 "; $result = mysql_query($query); confirm_query($result); if (mysql_num_rows($result) == 1) { //Redirect // Success } else { $error = "Sorry your username or password is incorrect.<br />"; } } } else { $message = "Please enter your username and password to log in"; } Edited November 21, 2013 by Joshua F Quote Link to comment https://forums.phpfreaks.com/topic/284141-changing-error-message-for-form-validation/#findComment-1459416 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.