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<br><br>';
}
if ($pass == '') {
	$error[] = 'Please fill in your password<br><br>';
}
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><Br>';
	}
mysql_close($db);
}
?>

I've tried various things with the meta tag, including updating multiple parts of the page (using ID|<blah> 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!

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('|');
[B]            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';
           }
[/B]        }
    }
}

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<br><br>';
}
if ($pass == '') {
	$error[] = 'Please fill in your password<br><br>';
}
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;
		[B]echo 'refresh|blahblah'; //this will signal to refresh the page(You could use any text you want)[/B]
	} else {
		echo 'loginMsg|<font color="#CC0000"><b>Invalid Username/Password!</b></font><br><Br>';
	}
mysql_close($db);
}
?>

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

Regards,
ivanceras

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.