Page 1 of 1

Register and Login (Email Activation) jQuery

Posted: Thu Jul 07, 2011 8:01 pm
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
[syntax=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>[/syntax]

register_checks.php WORKING

[syntax=php]<?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);
?>[/syntax]

register_checks.php NOT WORKING

[syntax=php]<?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);
?>[/syntax]

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.

Re: Register and Login (Email Activation) jQuery

Posted: Thu Jul 07, 2011 9:28 pm
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.

Re: Register and Login (Email Activation) jQuery

Posted: Fri Jul 08, 2011 9:38 am
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.

Re: Register and Login (Email Activation) jQuery

Posted: Sat Jul 09, 2011 2:38 pm
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.

Re: Register and Login (Email Activation) jQuery

Posted: Sat Jul 09, 2011 3:16 pm
by jacek
twiggy wrote:What is meant by this?

I don't use jQuery (or the lazy method as I called it).

Re: Register and Login (Email Activation) jQuery

Posted: Sat Jul 09, 2011 4:19 pm
by Kamal
Make sure '../../core/inc/register_checks.inc.php' is correct and relative to the URL of the page itself not the file.