Daryl B Posted July 28, 2007 Share Posted July 28, 2007 Hi Guys, I have Index.php which contains the HTML form with a username and password field + submit button, action: is my check.php. Say for example the user only input's a username and miss out the password field; when he clicks the submit button. I want the same page to be refreshed with a error message appearing below the submit button saying "error, please fill all in". I can get this to display on another page. but want this to be on refereshed on the log-in page. If anyone can help that would be great. Thanks ??? Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 28, 2007 Share Posted July 28, 2007 Post your form back to itself and do all of your error checking in the same script file. Quote Link to comment Share on other sites More sharing options...
Goose87 Posted July 28, 2007 Share Posted July 28, 2007 yeah, what i do is this: if (isset($_POST['submit'])) { // Check if the form has been submitted. require_once ('connect.php'); // Connect to the database if (empty($_POST['username'])) { // Validate the username. $u = FALSE; echo '<p>You forgot to enter your username!</p>'; } else { $u = ($_POST['username']); } if (empty($_POST['password'])) { // Validate the password. $p = FALSE; echo '<p>You forgot to enter your password!</p>'; } else { $p = ($_POST['password']); } if ($u && $p) { // Everything is ok. This checks that the fields have been entered before it checks if the username and password are correct. Then after that i query the database, and then i end the php and have the html form. Hope that helps, if not, i can give you my whole index script which will achieve what you want. Quote Link to comment Share on other sites More sharing options...
Daryl B Posted July 28, 2007 Author Share Posted July 28, 2007 cheers guys. I will give this a go and let you know how i get on. Looks good though Quote Link to comment Share on other sites More sharing options...
Daryl B Posted July 28, 2007 Author Share Posted July 28, 2007 Hey, cant seem to get it working: this is my code: <?php session_start(); include('db.php'); if(isset($_POST['submit'])) : // Check if the form has been submitted. // Username and password sent from signup form // First we remove all HTML-tags and PHP-tags, then we create a sha1-hash $username = strip_tags($_POST['username']); $password = sha1(strip_tags($_POST['password'])); // Make the query a wee-bit safer $query = sprintf("SELECT ID FROM members WHERE username = '%s' AND password = '%s' LIMIT 1;", mysql_real_escape_string($username), mysql_real_escape_string($password)); $result = mysql_query($query); if(1 != mysql_num_rows($result)) : // MySQL returned zero rows (or there's something wrong with the query) header('Location: index.php?msg=login_failed'); else : // We found the row that we were looking for $row = mysql_fetch_assoc($result); // Register the user ID for further use $_SESSION['member_ID'] = $row['ID']; header('Location: members-only.php'); endif; endif; if( ($username == null) or ($password == null) ) {header("location:index.php"); exit();} ?> Edited by SA: Please use the [code][/code] tags! Quote Link to comment Share on other sites More sharing options...
zq29 Posted July 29, 2007 Share Posted July 29, 2007 What do you mean by "cant seem to get it working"? What's it (not) doing? Side note: I didn't know that was valid syntax for if constructs in PHP, couldn't find anything in the manual - Where did you learn to use them that way?! Quote Link to comment Share on other sites More sharing options...
Daryl B Posted July 29, 2007 Author Share Posted July 29, 2007 Regarding the if constructs in PHP: I have just learnt myself using Mike McGeth's PHP5 book and the net. It must be a valid syntax as it does work. Which part of the code did pick up on the syntax issue? I have sorted the orginal issue, thanks. You post helped. Quote Link to comment 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.