Hello,
I'm trying to accomplish the following:
1. Show login form in a jQuery lightbox
2. Use Ajax to send request to PHP script to check if user exists in database -- I'll only need to check for user's email address
3. If user exists show welcome message then redirect to the welcome page
4. If user doesn't exist then show registration form in the same lightbox
5. Again use Ajax/PHP to collect user registration data and save data to mySQL database, display thanks for registering message then redirect to welcome page
----
First, here's my php code to connect to database and do a mySQL select of the email field.
PHP Code:
//connect to db
$db = mysql_connect("localhost", "dbuser", "dbpw");
if(!$db) {
echo mysql_error();
}
$select_db = mysql_select_db("dbname");
if(!$select_db) {
echo mysql_error();
}
//get post data from ajax
$email = mysql_real_escape_string((string)$_POST['email']);
$query = mysql_query("SELECT `email` FROM `tblname` WHERE `email` = '$email' LIMIT 1");
$rows = mysql_num_rows($query);
if($rows <= 0){
// user doesn't exist show registration form
// then insert user inputted data into database
//$query = mysql_query("INSERT INTO `tblname` (`fname` ,`lname` ,`email`) VALUES ('$fname', '$lname', '$email')");
}else{
// at least one row has been found
header("Location: welcome.html");
exit;
}
Here's the AJAX:
Code:
var datastring = '&email='+ email;
$.ajax({
type: "POST",
url: "login.php",
data: datastring,
success: function(responseText) {
if(responseText == 1) {
//email exists in db
//display welcome message then redirect to welcome page
}
error: function(responseText) {
if(responseText == 0) {
//email doesn't exist in db so show registration form
}
}
});
What's working so far:
- Email validation
- Redirects user to the welcome page if email exists in database
Where I'm lost:
- Need to show login success message before redirecting to welcome page
- Need to display registration form if email doesn't exist in database then show thanks for registering message then redirect to welcome page
I would like to display both login and registration forms inside a jQuery lightbox. I would imagine the need for AJAX callbacks but I'm not sure.
Please can anyone help? Thank you.