0

I am trying to create a signup form that checks if the user exists in the database, I inserted a sample user and when I tried signing up with that user it didn't say its already been taken. What have I done wrong?

The JavaScript:

function formSubmit()
    {
    document.getElementById('email_valid').innerHTML = '';

    var temail=document.forms["signup_form"]["temail"].value.replace(/^\s+|\s+$/g, '');


      var atpos=temail.indexOf("@");
        var dotpos=temail.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=temail.length)
          {
          //alert("Not a valid e-mail address");
          setTimeout(function(){document.getElementById('email_valid').innerHTML = '<br/>Email must be valid...';},1000);
          var temailsub=0;
          }
          else
          {
          $.post('/resources/forms/signup/email.php',{email: temail}, function(data){
            document.getElementById('email_valid').innetHTML = data;
            if(data.exists){
                    document.getElementById('email_valid').innetHTML = '<br/>The email address you entered is already in use.';
                    var temailsub=0;
                }else{
                    var temailsub=1;
                }
            }, 'JSON');
          }
    if(temailsub==1e)
      {
        setTimeout(function(){document.getElementById("signup_form").submit();},1000);
      }
      else
      {

        return false;
      }
    }

The PHP file (email.php):

<?php
header('content-type: text/json');
require_once $_SERVER['DOCUMENT_ROOT']."/resources/settings.php";
$query = $pdo->prepare("SELECT * FROM users WHERE email=:email");
$query->execute(array(
    ":email"=> $_POST['email']
));
echo json_encode(array('exists' => $query->rowCount() > 0));
?>

I have checked and double checked the code, I still cannot see why its not detecting that the email has already been used... what do i need to do to fix this and avoid this in the future?

3
  • 1. Mail addresses don't have to be unique, unless that's your primary key for identifying the user. 2. Are you sure there is no whitespace attached to the mail address you are searching? Case sensitivity considered? Do more debugging by var_dumping stuff you come across and compare with your expectation. Commented Dec 3, 2012 at 23:26
  • 2
    Isn't innetHTML a typo? Twice in your $.post() function... Commented Dec 3, 2012 at 23:30
  • @jtherman, add this as answer and I'll mark it! Commented Dec 3, 2012 at 23:38

1 Answer 1

2

The problem is that PDOStatement::rowCount() returns the number of rows affected by the last SQL statement. You are performing a SELECT so this value will always be 0. SELECT does not affect any rows. Instead you need to count the number of rows:

$query = $pdo->prepare("SELECT COUNT(*) FROM users WHERE email=:email");
$query->execute(array(
    ":email"=> $_POST['email']
));
$rows = $query->fetchColumn();

echo json_encode(array('exists' => $rows);

Also from jtheman's comment above, you should replace innetHTML with innerHTML in your JavaScript.

Sign up to request clarification or add additional context in comments.

6 Comments

i see where you are going but its still not working... could I have made some other error?
WOW, i had a typo in my JavaScript!
I edited my post to mention your spelling mistake too. Looks like you had two mistakes, not just one.
Mike, done, marked as answer :) my IDE didnt pick it up as its a PHP IDE.
umm i just tried it again but the code you have given me always returns something... so it always says the email has been used...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.