Register and Login (Email Activation) jQuery

Post here is you are having problems with any of the tutorials.
Post Reply
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

Register and Login (Email Activation) jQuery

Post by twiggy »

Ok I have been following a tutorial from another site on how to jQuery/AJAX your input data so it checks if a username has been used or not. All is working great (so far) but in the included PHP file that connects to the DB and runs the SQL check I have entered the connection details, so I thought to tidy this up I will use the init.inc.php that I made in the login tutorial from here. One problem, when I include that file instead of putting the database details directly in it doesn't work. It connects to the DB fine no errors, it just doesn't return any results via jQuery. Any ideas?

Register.php
<head>
		<meta http-equiv="Content-Type" content="text/html; charset+utf-8" />
		<link rel="stylesheet" type="text/css" href="ext/css/style.css" />
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript">
        $ (document).ready(function() {
			$('#username_feedback').load('../../core/inc/register_checks.inc.php').show();
			
		$('#username').keyup(function(){
			$.post('../../core/inc/register_checks.inc.php', { username: user_register.username.value },
			function(result) {
				$('#username_feedback').html(result).show();
			});
		});
		});
        </script>
		<title><?php echo $page_title ?></title>
	</head>
	
	<body>
		<div>
			<?php 
				if (empty($errors) === false) {
					?>
					<ul>
						<?php
							foreach ($errors as $error){
							echo "<li>{$error}</li>";			
							}
						?>
					</ul>
				<?php
				}	
			?>
		</div>
		<form name="user_register" action="" method="post">
			<p>
				<label for"username">Username:</label>
				<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>">
                <div id="username_feedback"></div>
register_checks.php WORKING
<?php

mysql_connect('localhost', 'root', '');
mysql_select_db('advertise');

$username = mysql_real_escape_string($_POST['username']);

$check = mysql_query("SELECT user_name FROM users WHERE user_name='$username'");
echo $check_num_rows = mysql_num_rows($check);
?>
register_checks.php NOT WORKING
<?php

include('../init.inc.php');

$username = mysql_real_escape_string($_POST['username']);

$check = mysql_query("SELECT user_name FROM users WHERE user_name='$username'");
echo $check_num_rows = mysql_num_rows($check);
?>
I changed some database details around in the init.inc.php file and I was getting errors back in my register.php file about connecting to the DB so the register_checks.php is using the init.inc.php file to 'import' the DB credentials just for some reason its not outputting my SQL query.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login (Email Activation) jQuery

Post by jacek »

I cant help with the lazy jQuery approach, but I can offer a tip. If you browse to the page directly and eliminate the ajax for now, it will be easier to debug and see any error that you get.
Image
libeco
Posts: 104
Joined: Sat May 07, 2011 9:56 am

Re: Register and Login (Email Activation) jQuery

Post by libeco »

If you open your console (if you have Firebug installed in firefox) you will see AJAX requests and their results, which is also a good way to spot any errors.
twiggy
Posts: 58
Joined: Sat Jun 11, 2011 11:11 pm

Re: Register and Login (Email Activation) jQuery

Post by twiggy »

I cant help with the lazy jQuery approach
What is meant by this?

Don't matter I will leave it like this for now until I gain enough experience to go deeper with it.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Register and Login (Email Activation) jQuery

Post by jacek »

twiggy wrote:What is meant by this?
I don't use jQuery (or the lazy method as I called it).
Image
User avatar
Kamal
Posts: 123
Joined: Fri May 06, 2011 10:45 am
Contact:

Re: Register and Login (Email Activation) jQuery

Post by Kamal »

Make sure '../../core/inc/register_checks.inc.php' is correct and relative to the URL of the page itself not the file.
Post Reply