Jump to content

[SOLVED] processing ajax with php


ngreenwood6

Recommended Posts

i have 4 files:

 

index.php

<?php
include("includes/variables.php");
?>
<html>
<head>
<script language="javascript" type="text/javascript" src="includes/login.js"></script>
<link href="includes/style.css" rel="stylesheet" type="text/css" />
<title><?php echo $sitename; ?></title>
</head>

<body>

<center>

<h1>Login Here!</h1>

<form name="login_form">
<table id="login_table">
<tr>
<td>Username</td>
<td>:</td>
<td id="username"><input name="username" type="text"></td>
<td id="username_empty"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td id="password"><input name="password" type="password"></td>
<td id="password_empty"></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="button" name="submit_login" value="Log In" onClick="CheckLogin();"></td>
</tr>
</table>
</form>

</center>

</body>
</html>

 

check_user.php

<?php
//include the variables
include("includes/variables.php");

//define the variables from the form
$username = strtolower($_POST['username']);
$password = $_POST['password'];

//connect to the database
$connect = mysql_connect($host, $db_user, $db_pass) or die("could not connect");

//select the database
$select_db = mysql_select_db($db) or die("could not select database");

//query for the database
$query = "SELECT * FROM $table WHERE username='$username'";

//get the results
$results = mysql_query($query);

//number the rows
$num_rows = mysql_num_rows($results);

//put it into an array
$row = mysql_fetch_array($results);

//give the username and password from the database a variable
$row_username = $row['username'];
$row_password = $row['password'];

//set the initial process value
$process = TRUE;

//if the username is blank
if($username == "")
{
echo 1;
$process = FALSE;
}
//if the password is blank
if($password == "")
{
echo 2;
$process = FALSE;
}
//if the username is not in the database
if($num_rows < 1)
{
echo 3;
$process = FALSE;
}
//if the password is not correct
if ($password != $row_password)
{
echo 4;
$process = FALSE;
}
//if username is in database
if($usernamme == $row_username)
{
echo 5;
}
//if the password is correct
if($password == $row_password)
{
echo 6;
}



?>

 

login.js

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 DoCallback2(data)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open('POST', url2, 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', url2, 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);
        }
    }
}


var url = "check_user.php";
var url2 = "process_login.php";
var what = "LoginStatus(req.responseText)";

           function CheckLogin()
            {
                var username = document.login_form.username.value;
                var password = document.login_form.password.value;

                DoCallback("username="+username+"&password="+password);
            }

            function LoginStatus(Status)
            {
			var username = document.login_form.username.value;
			var password = document.login_form.username.value;

			if(Status == 12356){
				document.getElementById("username_empty").innerHTML="Please enter a username!";
				document.getElementById("password_empty").innerHTML="Please enter a password!";
			}
			if(Status == 2356){
				document.getElementById("username_empty").innerHTML="That username is not in the database!";
				document.getElementById("password_empty").innerHTML="Please enter a password!";
			}
			if(Status == 24){
				document.getElementById("username_empty").innerHTML="";
				document.getElementById("password_empty").innerHTML="Please enter a password!";
			}
			if(Status == 4){
				document.getElementById("username_empty").innerHTML="";
				document.getElementById("password_empty").innerHTML="That password is not correct!";
			}
			if(Status == 345){
				document.getElementById("username_empty").innerHTML="That username is not in the database!";
				document.getElementById("password_empty").innerHTML="That password is incorrect!";
			}
			if(Status == 6){
				document.getElementById("username_empty").innerHTML="";
				document.getElementById("password_empty").innerHTML="";

				DoCallback2("username="+username+"&password="+password);

			}
}

 

and process_login.php

<?php
//include the variables
include("includes/variables.php");

//get the username from the form
$username = strtolower($_POST['username']);

if($username != "")
{
header("Location: test.php");
}

?>

 

This is actually a lot simpler than it seems. All it does is check the variables and if they are not correct it displays the error without reloading the page. Then if everything is correct it is supposed to process the login(process_login.php) and go to test.php. The problem is that it is not going to test.php which means that it is not processing the page how i want it to. My question is how do I get the ajax to stop and just allow the form to be processed hence taking me to test.php when the username and password are correct.

 

Sorry for the long post but I figured I would show incase I was missing or had something incorrect. Any help is appreciated.

Link to comment
Share on other sites

Yeah, like I said the status code portion is working. The only thing is that when it is supposed to do the process_login.php it just stays on the same page. It will only go to the process_login.php page if everything is good. When it goes to the process_login.php page it is supposed to redirect them to test.php which is just set up to make sure that it is working at this point. Does anyone know how I can accomplish this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.