lostprophetpunk Posted January 21, 2009 Share Posted January 21, 2009 I think I am having trouble with my PHP form within my blog system. Well, when the form is submitted without any text in the two fields, the 'all fields must be filled in' error message is displayed correctly. However, if I put something in the username field and submit the form, the 'all fields must be filled in' error shows up first and then the correct error shows up after that. I think it might be to do with my code. Here is the code relating to the problem... Login Form... echo "<div id='loginresult' style='display:none;'></div>\n"; echo "<form name='loginsub' id='loginsub' method='post' action='logincheck.php'>\n"; echo "Username <input type='text' name='username' id='username' autocomplete='off' />\n"; echo "Password <input type='password' name='password' id='password' autocomplete='off' />\n"; echo "<input type='submit' value='Login' id='loginsubmit' name='loginsubmit' />\n"; echo "</form>\n"; logincheck.php... @include('connect.php'); $user = mysql_real_escape_string( $_POST['username'] ); $pass = mysql_real_escape_string( $_POST['password'] ); //password encryption snipped if($user && $pass){ $sql33 = "SELECT * FROM `users` WHERE `username`='$user'"; $res33 = mysql_query($sql33) or die(mysql_error()); if(mysql_num_rows($res33) == 1){ $epass = md5($pass); $sql2 ="SELECT * FROM `users` WHERE `username`='$user' AND `password`='$passp'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) == 1){ //successful login echo "Currently logging you in, $user\n"; $row = mysql_fetch_assoc($res2); $_SESSION['uid'] = $row['id']; echo "<script type='text/javascript'>setTimeout('location.reload(true)',3000)</script>\n"; }else { echo "<b>ERROR: Username or password incorrect</b>\n"; } }else { echo "<b>ERROR: Username or password incorrect</b>\n"; } }else { echo "<b>ERROR: All fields are required</b>\n"; } This might also have something to do with the jQuery that I am using, but I am not entirely sure, so here is the jQuery related to the problem just in case it is that... $(document).ready(function(){ $("#loginsubmit").click(function(){ $("#loginresult").slideDown("fast"); var username = $('#username').val(); var password = $('#password').val(); $.ajax({ url: 'logincheck.php', type: 'POST', data: 'username=' + username + '&password=' + password, success: function(result) { $('#response').remove(); $('#loginresult').append('<p id="response">' + result + '</p>'); setTimeout('$("#loginresult").slideUp("fast")',4000); } }); return false; }); }); If someone could help me on this one, I would be so grateful. Quote Link to comment https://forums.phpfreaks.com/topic/141784-php-form/ Share on other sites More sharing options...
premiso Posted January 21, 2009 Share Posted January 21, 2009 Not sure if this will work but try this: $user = (isset($_POST['username']) && !empty($_POST['username'])) ? mysql_real_escape_string( $_POST['username'] ) : false; $pass = (isset($_POST['password']) && !empty($_POST['password'])) ? mysql_real_escape_string( $_POST['password'] ) : false; That way it may return the right code. Unsure, but yea, either way that is a better way of doing it imo. Quote Link to comment https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742291 Share on other sites More sharing options...
lostprophetpunk Posted January 21, 2009 Author Share Posted January 21, 2009 Nope, that doesn't work...it just does the same thing. The data is submitted asynchronously to 'logincheck.php' via the jQuery by the way. Quote Link to comment https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742319 Share on other sites More sharing options...
premiso Posted January 21, 2009 Share Posted January 21, 2009 Thinking about it, do you ever clear the div field of it's old results? I bet that would do the trick. Because there you are just appending it. I do not know exactly how it is called, but yea. If they are two separate errors form 2 different requests, that is your answer, you should clear the result div before appending the new text. Quote Link to comment https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742328 Share on other sites More sharing options...
lostprophetpunk Posted January 21, 2009 Author Share Posted January 21, 2009 Thinking about it, do you ever clear the div field of it's old results? I bet that would do the trick. Because there you are just appending it. I do not know exactly how it is called, but yea. If they are two separate errors form 2 different requests, that is your answer, you should clear the result div before appending the new text. How would I do that though? Quote Link to comment https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742417 Share on other sites More sharing options...
premiso Posted January 21, 2009 Share Posted January 21, 2009 If this does not work, look up an example via jQuery: $.ajax({ url: 'logincheck.php', type: 'POST', data: 'username=' + username + '&password=' + password, success: function(result) { $('#response').remove(); $('#loginresult').html('<p id="response">' + result + '</p>'); setTimeout('$("#loginresult").slideUp("fast")',4000); } }); return false; }); }); That should trash the old stuff and replace it with the new. Quote Link to comment https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742419 Share on other sites More sharing options...
lostprophetpunk Posted January 21, 2009 Author Share Posted January 21, 2009 No, it still does the same thing. I have also found out that if I put something in the password field and nothing in the username field, it does the 'incorrect username/password combination' error first, then the other one after that. Quote Link to comment https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742493 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.