jigsawsoul Posted March 18, 2010 Share Posted March 18, 2010 Here is my page, which checks to see if the email and password fileds are empty which works fine. so what it does is check the username and returns to register.php is its blank. but i want it to check all fields then return which ones are blank. so if both are blank, ill get a message somewhat like; A email address is required. A password is required. Anyhelp would be great thanks <?php session_start(); ob_start(); include('../../resources/config.php'); include($config["paths"]["resources"] . 'opendb.php'); include($config["paths"]["resources"] . '_library/login.php'); $email = mysql_real_escape_string($_POST['email']); $password = mysql_real_escape_string($_POST['password']); if(empty($_POST['email'])){ $_SESSION["message"] = 'A email address is required.'; header ('Location: register.php'); exit(); } if(empty($_POST['password'])){ $_SESSION["message"] = 'A password is required.'; header ('Location: register.php'); exit(); } $result = mysql_query("SELECT email FROM projectlogin WHERE email = '$email'"); $emailcheck = mysql_num_rows($result) or die(mysql_error()); if($emailcheck > 0){ $_SESSION["message"] = 'This email address is already registered with us. Having trouble, you can <a href="recover.php">reset your password here</a>.'; header ('Location: register.php'); exit(); } include($config["paths"]["resources"] . 'closedb.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/195705-validate-form-help/ Share on other sites More sharing options...
premiso Posted March 18, 2010 Share Posted March 18, 2010 Just so you understand why it does not display them in a line, you do a redirect after each error. If you want to notify someone of all empty, you should collect the messages and then do a redirect if the message variable is not empty. include($config["paths"]["resources"] . '_library/login.php'); $_SESSION['message'] = ""; if (empty($_POST['email'])) $_SESSION['message'] .= "A email address is required.<br />"; if (empty($_POST['password'])) $_SESSION['message'] .= "A Password is required.<br />"; if (!empty($_SESSION['message'])) { header("Location: register.php"); exit(); } // moved this below, as there is no need to try and escape empty data $email = mysql_real_escape_string($_POST['email']); $password = mysql_real_escape_string($_POST['password']); // rest of the processing Should do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/195705-validate-form-help/#findComment-1028173 Share on other sites More sharing options...
jigsawsoul Posted March 18, 2010 Author Share Posted March 18, 2010 thanks but but say if message was filled in then it wouldn't redirect and show me the errors for the other two which aren't, how do i get around this? Quote Link to comment https://forums.phpfreaks.com/topic/195705-validate-form-help/#findComment-1028183 Share on other sites More sharing options...
premiso Posted March 18, 2010 Share Posted March 18, 2010 What? The message is only appended if the if statements have been ran. So only if one of those fields (or both) are empty, the message will be populated. That is the point of me encasing them in an if statement. Did you test the code? if (!empty($_SESSION['message'])) { header("Location: register.php"); exit(); } That portion of the code I posted sees if the message has been filled in (meaning that one of the fields were empty) if it has been filled in, then it will redirect. Else it continues on with the code. Quote Link to comment https://forums.phpfreaks.com/topic/195705-validate-form-help/#findComment-1028197 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.