Jump to content

Php Login With Ajax Submit Issue.--Noobie


DefunctExodus

Recommended Posts

This may be a very basic issues but i do not have very much experiance with ajax and php,

 

What i am trying to do is create a login feild on my website, Once the user clicks login if they are successfull the login feild will be replaced with the conent that the user is authroized to see.

 

I have already created my Login script and everything but calling it with ajax....is proving to be a headache

 

Any help would be greatly appretiated.

 

Here is the Code i have done up for my login

 

 

Login Index. Contains Ajax Code (I did not write it) as well as the php includes for the login

 

<script type="text/javascript">
var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}

</script>
<?php
define("IN_APP",0);
require("app.php");

if(!$blnLoggedin){

include("javascript:ajaxpage('pages/login.php','colTwo');");

} else {

include("javascript:ajaxpage('pages/home.php','colTwo');");

}
?>

 

 

 

Here is the Page that should be loaded Upon opening up index assuming the user has not been logged in

 

 

<?php
if(!defined("IN_APP")){ die(); }

if(isset($_POST['Submit'])){

//Get variables
$strUsername = $_POST['username'];
$strPassword = $_POST['password'];

//Check login
if(!$cMySQL->doLogin($strUsername, $strPassword)){

	$strError = "Username/password combination incorrect.";

} else {

	header("Location: index.php");

}

}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login</title>
</head>

<body>
<form method="post" action="index.php">
<table width="250" border="0" cellspacing="2" cellpadding="0" style="border: 1px solid black;">
  <tr style="background-color:#000000; color:#FFFFFF;">
    <th colspan="2" scope="col">Login</th>
  </tr>
  <tr>
    <td>Username:</td>
    <td><input name="username" type="text" id="username" /></td>
  </tr>
  <tr>
    <td>Password:</td>
    <td><input name="password" type="password" id="password" /></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><?php if(isset($strError)){ echo("<font color=\"red\">".$strError."</font><br/>"); } ?><input type="submit" name="Submit" value="Login" /></td>
  </tr>
</table>
</form>
</body>
</html>

 

If the user has been authenticated or succesfully loged in with the previous script it would them show them there page

 

 

Sorry if i was not supposed to all that code. I just hitting my head against the wall trying to get this to work so.

 

Link to comment
Share on other sites

<?php
define("IN_APP",0);
require("app.php");

if(!$blnLoggedin){

include("javascript:ajaxpage('pages/login.php','colTwo');");

} else {

include("javascript:ajaxpage('pages/home.php','colTwo');");

}
?>

 

This is ALL wrong!

 

PHP is server side

Javascript and HTML is client side

 

include("javascript:..") wont work, thats only going to come  back file not found.

 

I think what your after is not aJax but just a different menu for logged in and not logged in.

 

<html>
<head><title>My Website</title></head>
<body>
Menu: <?php
if($bLoggedIn)
  include('loggedin_menu.php');
else
  include('loggedout_menu.php');
?>
<br>
Page Body
</body>

 

This will depending on the bLoggedIn variable show a different output for a menu.

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.