The Little Guy Posted July 27, 2007 Share Posted July 27, 2007 Is it possible to send back some variable for me to check if a login was successful? This way I can change the navigation, if they successfully logged in, or leave it as is if they did not successfully login. The two functions to create my complete AJAX: function getVal(){ email = document.getElementById('email').value; password = document.getElementById('password').value; val = 'pass='+password+'&email='+email+'&processType=fastLog'; return val; } function logUser(){ var contentType = "application/x-www-form-urlencoded; charset=UTF-8"; var ajaxRequest; try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ alert("Your Browser Doesn't support AJAX."); return false; } } } ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState < 4){ document.getElementById('loginForm').innerHTML = 'Logging in...'; } if(ajaxRequest.readyState == 4){ document.getElementById('loginForm').innerHTML = ajaxRequest.responseText; document.getElementById('loginForm').style.display = 'block'; } } va = getVal(); ajaxRequest.open("POST", '/includes/process/checkLogin', true); ajaxRequest.setRequestHeader("Content-Type", contentType); ajaxRequest.send(va); } The PHP to check if this is a good login: <?php include '../include.php'; if(!isLogged()){ $email = addslashes($_POST['email']); $pass = addslashes($_POST['pass']); $sql = mysqli_query($db,"SELECT * FROM users WHERE `email`='$email' AND `password`=PASSWORD('$pass')")or die(mysqli_error($db)); $row = mysqli_fetch_array($sql); if($row){ if($row['activations'] == 'inactive'){ $_SESSION['logged'] = FALSE; $_SESSION['message'] = 'This account has not been activated yet.'; header("Location: http://publicsize.com"); exit; }else{ $_SESSION['logged'] = TRUE; $_SESSION['id'] = $row['id']; $_SESSION['first'] = stripslashes($row['fname']); $_SESSION['last'] = stripslashes($row['lname']); $_SESSION['email'] = stripslashes($row['email']); $_SESSION['date'] = stripslashes($row['date']); $_SESSION['gender'] = stripslashes($row['gender']); if($row['birthDate']!=NULL){ $_SESSION['bday'] = stripslashes($row['birthDate']); } if($row['displayName']!= NULL){ $_SESSION['displayName'] = stripslashes($row['displayName']); }else{ $_SESSION['displayName'] = NULL; } if(isset($_SESSION['message'])){ unset($_SESSION['message']); } mysqli_query($db,"UPDATE users SET lastLogin = '$date', onlineStatus = '1', `lastAccess` = UNIX_TIMESTAMP(NOW()) WHERE id = '{$_SESSION['id']}'")or die(mysqli_error($db)); if($_POST['processType'] == 'fastLog'){ echo 'Logged in as: <strong>'; if($_SESSION['displayName'] == NULL){ echo $_SESSION['first']; }else{ echo $_SESSION['displayName']; } echo'</strong>'; $testvar = 'I can read'; }else{ header("Location: http://publicsize.com"); exit; } } }else{ if($_POST['processType'] == 'fastLog'){ echo '<p>Incorrect Email or Password.</p> <form action="javascript:logUser();" method="post"> <p> Email:<br /> <input id="email" type="text" onfocus="javascript:Look1(\'email\');" onblur="javascript:Look2(\'email\');" /> </p> <p> Password:<br /> <input id="password" type="password" onfocus="javascript:Look1(\'password\');" onblur="javascript:Look2(\'password\');" /> </p> <p> <input type="submit" name="submit" value="Login" /> </p> </form> '; }else{ $_SESSION['message'] = 'Incorrect Email or Password.'; header("Location: http://publicsize.com"); exit; } } }else{ if($_POST['processType'] == 'fastLog'){ echo 'Unexpected error'; }else{ header("Location: http://publicsize.com"); exit; } } ?> Link to comment https://forums.phpfreaks.com/topic/62105-return-a-variable/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.