NArc0t1c Posted August 18, 2007 Share Posted August 18, 2007 Hello there, I have an ajax login script, My problem is it is always returning the same variable even if the response is incorrect. I have it posting to a php script that makes use of a php class in another script. Here is my ajax login, it is two parts, Ajax.js and the actual login form, it is in a php class, i just removed it. Ajax.php, I think this is fine. function DoCallback(data) { // branch for native XMLHttpRequest object if (window.XMLHttpRequest) { req = new XMLHttpRequest(); req.onreadystatechange = processReqChange; req.open('POST', url, true); req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); req.send(data); // branch for IE/Windows ActiveX version } else if (window.ActiveXObject) { req = new ActiveXObject('Microsoft.XMLHTTP') if (req) { req.onreadystatechange = processReqChange; req.open('POST', url, true); req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); req.send(data); } } } function processReqChange() { // only if req shows 'loaded' if (req.readyState == 4) { // only if 'OK' if (req.status == 200) { eval(what); } else { alert('There was a problem retrieving the XML data:\n' + req.responseText); } } } And the login form with its javascript, ignore the php $Display variable. <script> var url = "Login_auth.php"; var what = "LoginStatus(req.responseText)"; function CheckLogin() { document.getElementById("status").className = "login_form"; var username = document.getElementById("username").value; var password = document.getElementById("password").value; document.getElementById("status").innerHTML = "Checking.."; if (!username){ document.getElementById("status").innerHTML = "Username was left empty."; document.getElementById("status").className = "login_form_fail"; } else if (!password){ document.getElementById("status").innerHTML = "No password was given."; document.getElementById("status").className = "login_form_fail"; } else { DoCallback("username="+username+"&password="+password); document.getElementById("status").className = "login_form"; } } function LoginStatus(Status){ document.getElementById("password").value = ""; if(Status == "0") document.getElementById("status").innerHTML = "Invalid Username."; else if(Status == "1") document.getElementById("status").innerHTML = "Invalid Password."; else document.getElementById("status").innerHTML = "Correct."; } </script> <div class="' . $Display .'"> <div id="status" class="login_form"></div> <pre>Username: <input id="username" type="text"><br>Password <input id="password" type="password"><br><br><input type="button" value="Check Login" onClick="CheckLogin()"> </pre> </div>'; I have the script Ajax.js linked in the header script. I'm also giving the php script Login_auth.php, <?php include("Classes/Initialize.Class.php"); if (isset($_POST)){ $UserName = $Misc->CleanString($_POST['username']); $UserPassword = $Misc->CleanString($_POST['password']); } elseif (isset($_GET)){ $UserName = $Misc->CleanString($_GET['username']); $UserPassword = $Misc->CleanString($_GET['password']); } else { echo 'Hacking Attempt..'; exit; } $Login->Authenticate($UserName, $UserPassword); ?> Thanks Ferdi Link to comment https://forums.phpfreaks.com/topic/65596-always-true-statement/ Share on other sites More sharing options...
ScotDiddle Posted September 13, 2007 Share Posted September 13, 2007 Ferdi, I placed a couple of alert statements in your code.... In : processReqChange() evalOut = eval(what); alert('eval(what) : ' + evalOut) // Displays second as: eval(what) : undefined First Line in In: LoginStatus(Status) alert('Status: ' + Status); // Displays first as Status: Hope this helps. Scot L. Diddle, Richmond VA Link to comment https://forums.phpfreaks.com/topic/65596-always-true-statement/#findComment-347672 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.