Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/141784-php-form/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742291
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742328
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742417
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/141784-php-form/#findComment-742419
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.