Jump to content

Asynchronous login form with jQuery and PHP


lostprophetpunk

Recommended Posts

I seriously did not know where to post this as it has jQuery (which is javascript in respect) and PHP within the problem...so please move it if you can find the appropriate forum.

 

Hello, I have just recently completed a blog system with PHP. I am now

enhancing it with jQuery, but I have come across some problems.

 

My login form is in a toggle div, so I submit the form info

asynchronously to the db via jQuery and it's ajax features. However

when I do this the form does everything correctly, and reloads the

page...but for some reasons the sessions do not work, therefore you

are not logged in.

 

I have also noticed that a javascript reload will work with jQuery but

an html meta refresh will not, does anyone know why this is?

 

The code for my login form is this...

 

<?php

    echo "<form name='loginsub' method='post' action='db.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' />\n";
    echo "</form>\n";

?>

 

The code for db.php is this...

 

<?php

  @include('connect.php');

    $user = $_POST['username'];
    $pass = $_POST['password'];

    $pass = md5( $pass );
    $passm = sha1( $pass );
    $passn = md5( $passm );
    $passo = sha1( $passn );
    $passp = md5( $passo );

    if($user && $pass){
        $sql = "SELECT * FROM `users` WHERE `username`='$user'";
        $res = mysql_query($sql) or die(mysql_error());

          if(mysql_num_rows($res) == 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";
                          //echo "<meta http-equiv='refresh' content='4' />\n";

              }else {
    echo "<b>Username or password incorrect</b>\n";
              }
           }else {
    echo "<b>Username or password incorrect</b>\n";
           }
       }else {
    echo "<b>All fields need to be filled in</b>\n";
      }

?>

 

Then finally the jQuery for the form is this...

 

$(function(){
  $('#loginsubmit').click(function(){

    var username = $('#username').val();
    var password = $('#password').val();

    $.ajax({
          url: 'db.php',
          type: 'POST',
          data: 'username=' + username + '&password=' + password,

          success: function(result) {
              $('#response').remove();
              $('#navloginf').fadeOut("slow");
              $('#nave').slideDown("fast");
              $('#nave').append('<p id="response">' + result + '</p>');

                          }
    });

    return false;

});
});

 

If anyone could help me out on why the sessions are not work, that

would be very nice.

Link to comment
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.