I am using JQUERY, AJAX, and PHP to 1) determine whether the user already exists, then 2) add the user if the user does not already exist. The way I've decided to try this is to use AJAX to run a PHP script that determines whether the user exists. If the query returns false, then the AJAX function runs another AJAX function that runs a PHP script to add the user.
I know that the "success:" part of the AJAX function works, because I originally had it generating alerts that showed the result of the query. The problem is when I try to put a second AJAX function in the "success:" area in order to add the user if the user does not exist.
I feel like this must have something to do with my query syntax for the "INSERT", but it looks right to me. I've searched the following pages just to make sure:
http://www.w3schools.com/sql/sql_insert.asp http://www.w3schools.com/php/php_mysql_insert.asp
Without further ado, here is my code.
HTML
<div id="btnRegister" class="btnRegisterInitial btnRaised trans500ms" style="z-index:3;">
<input id="email" class="txtInitial txtEmail trans500ms" style="font-family:arial; font-size:18px; color:#433343;" type="email" name="" value="" placeholder="">
<input id="password" class="txtInitial txtPassword trans500ms" style="font-family:arial; font-size:18px; color:#433343;" type="password" name="" value="" placeholder="">
JQUERY / AJAX:
$("#btnRegister").click(
function() {
validateUserInput(); // PLACES VALUE IN INVISIBLE INPUT IF USER INPUT IS VALID
if ($("#inputValid").val()) {
// alert("user's input is valid. User wants to register.");
$("#inputValid").val("");
$.ajax({ // AJAX TO CHECK IF USER EXISTS
url: 'UserExists.php',
type: 'post',
dataType: 'json',
data: {email: $('#email').val(), password: $('#password').val()},
success: function(data) {
if (data) { // IF RESULT OF QUERY HAS A VALUE (IT WILL IF EMAIL INPUT WAS FOUND IN DB)
alert("you exist");
} else {
alert("so, you new here?");
$.ajax({ // AJAX TO ADD USER IF USER DOESN'T ALREADY EXIST
url: 'Register.php',
type: 'post',
dataType: 'json',
data: {email: $('#email').val(), password: $('#password').val()},
success: function(data) {}
});
}
}
});
}
});
UserExists.PHP:
<?php
// SET DATABASE LINK AS A VARIABLE THAT CAN BE USED WHEN RUNNING QUERIES
$link = mysqli_connect(...db stuff...);
if (mysqli_connect_error()) { // AVOIDS ECHOING TOO MUCH SENSITIVE INFO ABOUT DATABASE ON ERROR
die("Could not connect");
}
$uiemail = mysqli_real_escape_string($link, $_POST['email']); //CREATE VARIABLE RATHER THAN WORRYING ABOUT QUOTES IN QUERY
$uipassword = mysqli_real_escape_string($link, $_POST['password']);
// SELECT all fields FROM databasename
$query = "SELECT * FROM users WHERE email='$uiemail'"; //SIMPLIFIES BY USING VARIABLE RATHER THAN WORRYING ABOUT STUPID QUOTES
// CREATE ARRAY CONTAINING ALL VALUES FOR ROW MATCHING QUERY CRITERIA
$row = mysqli_fetch_array(mysqli_query($link, $query));
$name = $row['name'];
$error = 'I could not find that user name';
if($row){
echo json_encode($name);
} else {
echo json_encode("");
}
?>
Register.PHP
<?php
$link = mysqli_connect("localhost", "web205-example", "qfz!ghWHd", "web205-example");
if (mysqli_connect_error()) {
die("Could not connect");
}
$uiemail = mysqli_real_escape_string($link, $_POST['email']); //CREATE VARIABLE RATHER THAN WORRYING ABOUT QUOTES IN QUERY
$uipassword = mysqli_real_escape_string($link, $_POST['password']);
// ENTERS NEW USER INTO TABLE
$query = "INSERT INTO `users` (`email`, `password`) VALUES ($uiemail, $uipassword)";
mysqli_query($link, $query);
?>
Yes, I know that I'm doing form stuff in an unconventional way. This is just how I wanted to try it. I think the problem must be with my AJAX or my PHP queries.
I tried to include most of the code. Let me know if you need me to simplify by including only the code that I think is important.
Thanks!
Thanks for the input. I'm not exactly sure what the problem was, but I got it to work after playing around with the JQUERY / AJAX: section (shown above). Here are the things I did that led to it finally working:
Then it began working. So, while I haven't verified my theory, I'm 90% sure that if you were to really examine the JQUERY / AJAX: section above, you would find that there is a problem with the curly brackets or parentheses (which can be a hard problem to spot). So I guess the lesson for me is: Double and triple check the brackets / parentheses if you're copying-pasting code around.
So I just wanted to post this so that anyone looking at this will know how it was fixed.
Thanks everyone for the input.