
Aquí dejo el código:
ajax.js
Código :
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: ' + req.responseText); } } }
login.php
Código :
<?php if(isset($_POST["username"]) && isset($_POST["password"])) { $username = $_POST["username"]; $password = $_POST["password"]; if($username = "user" && $password == "password") echo 1; else echo 0; } else { echo 0; } ?>
Página del logeo
Código :
<html> <head> <script> var url = "login.php"; var what = "LoginStatus(req.responseText)"; function CheckLogin() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; DoCallback("username="+username+"&password="+password); } function LoginStatus(Status) { if(Status == 0) alert("Bad login!"); else alert("Login OK!"); } </script> <script src="ajax.js" type="text/javascript"></script> </head> <body> <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> </body> </html>