Forum Moderators: open

Message Too Old, No Replies

Ajax Login script help!

         

ToxinMan

6:33 am on Jan 30, 2008 (gmt 0)

10+ Year Member



Hey there
I was wondering if anyone will be willing to have a look at my Ajax/php login script to see if anything is going wrong?Please

The thing is, im trying to have it so that once this function gets triggered on index.html:

<script type="text/javascript">
function loginsubmit(){

var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if(username == ""){
alert("Please enter your username!");
return false;}
if(password == ""){
alert("Please enter your password!");
return false;}

else{
postRequest();}

}
</script>

and everything is correct it triggers this ajax function:

<script type="text/javascript">

function postRequest(){
var xmlHttp;
if(window.XMLHttpRequest){ // For Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject){ // For Internet Explorer
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open('POST',"login.php", true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4){
updatepage(xmlHttp.responseText);
}
}
xmlHttp.send(null);
}

function updatepage(){
if($gears =="yes"){
alert("Welcome User");
}else{
alert("Invalid Login! Please try again!");
}
}

</script>

which gets the contents from this php file:

<?php
$username = $_GET["username"];
$password = $_GET["password"];

if($username == "admin" && $password == "admin"){
$gears = "yes";}
else{
$gears = "no";}

?>

so it can tell ajax or html which pop up (alert) to choose all without refreshing

THANKS HEAPS!
Ricky

ToxinMan

8:44 am on Jan 30, 2008 (gmt 0)

10+ Year Member



no ones helping :(

mehh

5:57 pm on Jan 30, 2008 (gmt 0)

10+ Year Member



change your updatepage function to:
function updatepage(re){ 
if(re =="yes"){
alert("Welcome User");
}
else{
alert("Invalid Login! Please try again!");
}
}
and your php file to
<?php 
$username = $_GET["username"];
$password = $_GET["password"];
if($username == "admin" && $password == "admin"){
echo "yes";
}
?>
Be patient, this isn't IM.