Jump to content

PHP + AJAX Login issues


james182

Recommended Posts

i'm trying to get this ajax login script to work. getting no response from ajax.

 

Please if someone can fix it, that would be great.

 

jquery.js:

/***************************************
* LOGIN AJAX
***************************************/
$(document).ready(function() {
$('#errorConsole').hide();

$('form[name=admin_login]').submit(function() {
	$('#errorConsole').slideUp();

	$.post('../login.php', { username: $('[name=username]').val(), password: $('[name=password]').val() },

	function(data) {
		if(data.success) 
		{
			location.href = data.redirect;
		} 
		else 
		{
			$('#errorConsole').html(data.message).slideDown();
		}
	}, 'json');

	return false;
});
});

 

login.php:

if($_REQUEST['action'] == "login"){
	if($log->login("logon", $_REQUEST['username'], $_POST['password']) == true){
		//do something on successful login
		$data['success'] = true;
		$data['redirect'] = '#';
	}else{
		//do something on FAILED login
		echo 'WRONG ... ';
		$data['success'] = false;
		$data['message'] = 'Oops';
	}

	echo json_encode($data);
}

 

Index.php:

<div class="box box-50 altbox">
		<div class="boxin">
			<div class="header">
				<h3><img src="_css/img/logo-login.png" alt="CMS Admin" /></h3>
				<ul>
					<li><a href="#" class="active">login</a></li><!-- .active for active tab -->
					<li><a href="#">lost password</a></li>
				</ul>
			</div>

			<FORM id="'.$formname.'" class="'.$formclass.'" method="post" name="'.$formname.'" action="'.$formaction.'" >
			<!-- Default forms (table layout) -->
				<div class="inner-form">

					<div id="errorConsole" class="msg msg-error"></div>

					<table cellspacing="0">

    * Notify me of replies.
						<tr>
							<th><label for="username">Username:</label></th>
							<td><input class="txt" type="text" id="username" name="username" value="demo" /></td>
						</tr>
						<tr>
							<th><label for="password">Password:</label></th>
							<td><input class="txt pwd" type="password" id="password" name="password" value="demos" /></td><!-- class error for wrong filled inputs -->
						</tr>
						<tr>
							<th></th>
							<td class="tr proceed">
								<input id="action" value="login" type="hidden" name="action">
								<input class="button" type="submit" value="Log in" />
							</td>
						</tr>
					</table>
				</div>
			</form>
		</div>
	</div>

 

 

Link to comment
Share on other sites

You'll want to give us an error to look at where the problem is occuring - don't think many forum goers want to check your syntax for a missing bracket etc..

 

As thorpe said try using firebug, the plug-in for firefox. Are you sure it is connecting to the database correctly? If you have mysql errors off, it could be an issue.

 

 

Anyway, anymore information you can provide will help you get an answer a lot. Debug/break it down and try work out what is working in the script and what is not.

Link to comment
Share on other sites

It all looks fine from a syntax point of view, a few things to check are:

 

[*]is the login.php form one directory up?

[*]in the login.php, if its sucessfull the data.redirect will be a #, so it wont actually take you anywhere, should this be the name of a page (i.e home.php)

[*]also check that $forname etc has what you expect in there, else the bindings wont work

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.