Refreshing a Page with PHP through Ajax

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Sep 2008
Posts: 3
Reputation: animedude123 is an unknown quantity at this point 
Solved Threads: 0
animedude123 animedude123 is offline Offline
Newbie Poster

Refreshing a Page with PHP through Ajax

 
0
  #1
Nov 14th, 2008
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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function createRequestObject() {
  2. var ro;
  3. var browser = navigator.appName;
  4. if(browser == "Microsoft Internet Explorer"){
  5. ro = new ActiveXObject("Microsoft.XMLHTTP");
  6. }else{
  7. ro = new XMLHttpRequest();
  8. }
  9. return ro;
  10. }
  11.  
  12. var http = createRequestObject();
  13.  
  14. function login() {
  15. var Username = document.Login.loginUsername.value;
  16. var Password = document.Login.loginPassword.value;
  17. var URL = document.Login.URL.value;
  18. var params = 'Username='+Username+'&Password='+Password+'&URL='+URL+'';
  19. document.Login.loginBtn.disabled=true;
  20. document.Login.loginBtn.value='Validating';
  21.  
  22. http.open('POST', 'loginUser.php', true);
  23. http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  24. http.setRequestHeader("Content-length", params.length);
  25. http.setRequestHeader("Connection", "close");
  26. http.onreadystatechange = handleResponse;
  27. http.send(params);
  28. }
  29.  
  30. function handleResponse() {
  31. if(http.readyState == 4){
  32. var response = http.responseText;
  33. var update = new Array();
  34.  
  35. if(response.indexOf('|' != -1)) {
  36. update = response.split('|');
  37. document.getElementById(update[0]).innerHTML = update[1];
  38. document.Login.loginBtn.disabled=false;
  39. document.Login.loginBtn.value='Try Again';
  40. }
  41. }
  42. }

Here is my PHP File:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?php
  2. include_once("config.php");
  3. $user = mysql_real_escape_string(stripslashes($_POST['Username']));
  4. $pass = mysql_real_escape_string(stripslashes($_POST['Password']));
  5. $URL = $_POST['URL'];
  6. if ($user == '') {
  7. $error[] = 'Please fill in your username<br><br>';
  8. }
  9. if ($pass == '') {
  10. $error[] = 'Please fill in your password<br><br>';
  11. }
  12. if (count($error) != '0') {
  13. for($x=0;$x<count($error);$x++) {
  14. $errors .= $error[$x];
  15. }
  16. echo 'loginMsg|<font color="#CC0000"><b>'.$errors.'</b></font>';
  17. } else {
  18. $result = mysql_query("SELECT * FROM lum_user WHERE Name='$user' AND Password='$pass' LIMIT 1")or die(mysql_error());
  19. $row = mysql_fetch_assoc($result);
  20. $rows = mysql_num_rows($result);
  21. if ($rows == '1') {
  22. $_SESSION['login_id'] = md5(base64_encode($user).base64_encode($pass));
  23. $_SESSION['login_user'] = $user;
  24. $_SESSION['login_pass'] = $pass;
  25. echo '<meta http-equiv="refresh" content="0;url='.$URL.'">';
  26. } else {
  27. echo 'loginMsg|<font color="#CC0000"><b>Invalid Username/Password!</b></font><br><Br>';
  28. }
  29. mysql_close($db);
  30. }
  31. ?>

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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 20
Reputation: ivanCeras is an unknown quantity at this point 
Solved Threads: 1
ivanCeras's Avatar
ivanCeras ivanCeras is offline Offline
Newbie Poster

Re: Refreshing a Page with PHP through Ajax

 
0
  #2
Nov 16th, 2008
add this function to your javascript
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function reloadPage(){
  2. location.reload(true)
  3. }

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('|');
            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';
           }
        }
    }
}

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;
		echo 'refresh|blahblah'; //this will signal to refresh the page(You could use any text you want)
	} 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
ivan_ceras at_yahoo_dot_com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC