1

I have been stuck on this problem for two days now. I've found the code to post data without refresh but it seems that if it doesn't refresh, the values aren't actually saved to the $_SESSION superglobal.

my jquery code:

       $('.login_error').hide();
   $('#sublogin').click(function(){
   $('.login_error').hide();

   var jusername = $('#loginusername').val();
   var jpassword = $('#loginpassword').val();


   if(jusername == ""){        
       $('label#username_error').show();
   //return false;
   }       
   if(jpassword == ""){
       $('label#password_error').show();    
    return false;
   }
   var dataString = 'loginusername='+jusername+'&loginpassword='+jpassword;
    $.post('login.php', dataString, function(data) {
            $('#header').html("<div id='message'></div>");  
    $('#message').html("<h2>Contact Form Submitted!</h2>")  
    .append("<p>We will be in touch soon.</p>")  
    .hide()  
    .fadeIn(1500)   
    });
    //   return false
});

My login.php code:

<?php

//set the variables
$username   = isset($_POST['loginusername'])? $_POST['loginusername']:'';
$password   = isset($_POST['loginpassword'])? $_POST['loginpassword']:'';
$step       = isset($_POST['step']) ? $_POST['step'] : '1';
if($step=='2'){
    //validation is done in javascript
    //if there are no errors
    if(empty($loginErrors)){
    //sanitizes data for use in query.
    $username = trim(mysql_real_escape_string($username));
    $password = md5(trim(mysql_real_escape_string($password)));
    $query = "SELECT user_level, username, password FROM user WHERE username='$username' AND password='$password'";     
    $result = mysql_query($query) or die('query did not go through');       
    if($result!=false){         
        $query_row = mysql_fetch_assoc($result);
        $_SESSION['user_level']=$query_row['user_level'];
        $_SESSION['username']=$query_row['username'];
        $_SESSION['password']=$query_row['password'];
    }
    }
}
if($step=='1'){         

?>

<form class="login_fields" id="login_fields" name="login" method="POST">
    <input type="hidden" name="step" value="2"/>
    <div class="dtable">
    <div class="dtr">
        <span class="dtd"> 
        <label name="name_label" for="username_box"> Username </label> 
        </span>
        <span class="dtd">
        <input type="text" id="loginusername" name="loginusername" maxlength="25" value=""/>
        </span>
        <span class="tdt">
        <label for="username" id="username_error" class="login_error">Username field can not be blank</label>
        </span>
    </div>
    <div class="dtr"> 
        <span class="dtd"> 
        <label name="pass_label" for="username_box"> Password </label>
        </span>
        <span class="dtd">
        <input type="password" id="loginpassword" name="loginpassword" maxlength="20" value=""/> 
        </span>
        <span class="tdt">
        <label for="password" id="password_error" class="login_error">Password field can not be blank</label>
        </span>
    </div>
    <div class="dtr">
        <span class="dtd">
        <input type="submit" name="sublogin" id="sublogin" value="login"/>
        </span>
    </div>
    </div>
    <div class="output" id="output" name="output">
    </div>
</form>
<?php
}
?>

Edit: Ah yes I forgot to mention. login.php is included in my headers file and my headers file is included in my index.php file so the session_start should cascade.

Edit 2: So if I set the return to false near the last line of the jquery code the page doesn't reload which is exactly what I want but the session isn't saved at all. Anyone able to figure out whats wrong with what I'm doing?

3
  • 2
    Where is your session_start();??? Commented Feb 11, 2012 at 21:19
  • you are doing this $.post('login.php'.... login.php has no session_start() like already mentioned. If session_start(); is in your header file, you either have to include that file or send the post request to index.php, but you have to declare session_start(); somewhere. Commented Feb 14, 2012 at 1:28
  • I said in my edit that the session_start is cascaded down from my index.php which includes the headers.php... and my headers.php includes login.php. Commented Feb 14, 2012 at 6:40

1 Answer 1

2

You should declare session_start(); at the top of your PHP script, otherwise the SESSION data won't be remembered!!

Sign up to request clarification or add additional context in comments.

1 Comment

Ah yes I forgot to mention. login.php is included in my headers file and my headers file is included in my index.php file so the session_start should cascade.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.