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?
session_start();???