| | |
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
![]() |
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Solved Threads: 0
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:
Here is my PHP File:
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!
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)
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<?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
then edit your handleResponse script
then edit also your php file
Improve the code. At least it gives you a hint.
Regards,
ivanceras
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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('|');
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
![]() |
Similar Threads
- creating my version of a photo album (PHP)
- get html element value using php (PHP)
- AJAX : Am i right with the concept? (JavaScript / DHTML / AJAX)
- Submit or action button (JavaScript / DHTML / AJAX)
- AJAX techniques... (JavaScript / DHTML / AJAX)
- 100% New! (Community Introductions)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: trying to append value to textarea with select box
- Next Thread: Ajax referencing a column and fields
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxexample ajaxjspservlets array blackjack browser captcha captchaformproblem cart child close codes css date debugger decimal dependent design developer disablefirebug dom element embed engine enter error events ext file firefox flash focus form forms frameworks game getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe index java javascript javascripthelp2020 jquery jsp jump libcurl listbox maps marquee masterpage math media menu mp4 onerror onmouseoutdivproblem onmouseover onreadystatechange parameters paypal pdf php position post problem programming prototype rated rating redirect safari scale scriptlets scroll search security size software star starrating stretch synchronous toggle tweet unicode variables w3c web webservice window \n





