954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Refreshing a Page with PHP through Ajax

Hey,

Right now I'm working on a login system for my site.

The user enters their info, the info is sent to the php page through ajax, the php checks if the input fields are empty and checks if the info is correct.

If fields are empty or the info is wrong, it updates a div layer with an error message telling them what is wrong.

My problem though is if the user enters correct info. If the user enters correct info I want the main page (index.php) to be refreshed (This is the page with the original form on it).

Anyone know how to do this?

This is my javascript:

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function login() {
	var Username = document.Login.loginUsername.value;
	var Password = document.Login.loginPassword.value;
	var URL = document.Login.URL.value;
	var params = 'Username='+Username+'&Password='+Password+'&URL='+URL+'';
	document.Login.loginBtn.disabled=true; 
	document.Login.loginBtn.value='Validating';

    http.open('POST', 'loginUser.php', true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
    http.onreadystatechange = handleResponse;
    http.send(params);
}

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            document.getElementById(update[0]).innerHTML = update[1];			
			document.Login.loginBtn.disabled=false; 
			document.Login.loginBtn.value='Try Again';
        }
    }
}


Here is my PHP File:

<?php
include_once("config.php");
$user = mysql_real_escape_string(stripslashes($_POST['Username']));
$pass = mysql_real_escape_string(stripslashes($_POST['Password']));
$URL = $_POST['URL'];
if ($user == '') {
	$error[] = 'Please fill in your username';
}
if ($pass == '') {
	$error[] = 'Please fill in your password';
}
if (count($error) != '0') {
	for($x=0;$x<count($error);$x++) {
		$errors .= $error[$x];
	}
	echo 'loginMsg|<font color="#CC0000"><b>'.$errors.'</b></font>';
} else {
	$result = mysql_query("SELECT * FROM lum_user WHERE Name='$user' AND Password='$pass' LIMIT 1")or die(mysql_error());
	$row = mysql_fetch_assoc($result);
	$rows = mysql_num_rows($result);
	if ($rows == '1') {
		$_SESSION['login_id'] = md5(base64_encode($user).base64_encode($pass));
		$_SESSION['login_user'] = $user;
		$_SESSION['login_pass'] = $pass;
		echo '<meta http-equiv="refresh" content="0;url='.$URL.'">';
	} else {
		echo 'loginMsg|<font color="#CC0000"><b>Invalid Username/Password!</b></font><Br>';
	}
mysql_close($db);
}
?>


I've tried various things with the meta tag, including updating multiple parts of the page (using ID| instead of just a plain echo) and a couple other things. None of them will get the original page itself to reload.

Thanks for any help!

animedude123
Newbie Poster
3 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

add this function to your javascript

function reloadPage(){
    location.reload(true)
}


then edit your handleResponse script

function handleResponse() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
<strong>            if(update[0]=='refresh'){ //if the first part of text says refresh
                 reloadPage();//then reload the page
            }else{
            document.getElementById(update[0]).innerHTML = update[1];			
			document.Login.loginBtn.disabled=false; 
			document.Login.loginBtn.value='Try Again';
           }
</strong>        }
    }
}


then edit also your php file

<?php
include_once("config.php");
$user = mysql_real_escape_string(stripslashes($_POST['Username']));
$pass = mysql_real_escape_string(stripslashes($_POST['Password']));
$URL = $_POST['URL'];
if ($user == '') {
	$error[] = 'Please fill in your username';
}
if ($pass == '') {
	$error[] = 'Please fill in your password';
}
if (count($error) != '0') {
	for($x=0;$x<count($error);$x++) {
		$errors .= $error[$x];
	}
	echo 'loginMsg|<font color="#CC0000"><b>'.$errors.'</b></font>';
} else {
	$result = mysql_query("SELECT * FROM lum_user WHERE Name='$user' AND Password='$pass' LIMIT 1")or die(mysql_error());
	$row = mysql_fetch_assoc($result);
	$rows = mysql_num_rows($result);
	if ($rows == '1') {
		$_SESSION['login_id'] = md5(base64_encode($user).base64_encode($pass));
		$_SESSION['login_user'] = $user;
		$_SESSION['login_pass'] = $pass;
		<strong>echo 'refresh|blahblah'; //this will signal to refresh the page(You could use any text you want)</strong>
	} else {
		echo 'loginMsg|<font color="#CC0000"><b>Invalid Username/Password!</b></font><Br>';
	}
mysql_close($db);
}
?>


Improve the code. At least it gives you a hint.

Regards,
ivanceras

ivanCeras
Newbie Poster
20 posts since May 2006
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You