views:

153

answers:

1

Hello Everybody,

my problem is, I want to validate an email adresse with jquery. Not only the syntax but rather if the email is already registrated. There are some tutorials but they are not working! At first the Jquery Code:

<script id="demo" type="text/javascript"> 
$(document).ready(function() {
 // validate signup form on keyup and submit
 var validator = $("form#signupform").validate({
  rules: {
   Vorname: {
    required: true,
    minlength: 3
   },
   Nachname:{
    required: true,
    minlength: 4
   },
   password: {
    required: true,
    minlength: 5
   },
   password_confirm: {
    required: true,
    minlength: 5,
    equalTo: "#password"
   },
   Email: {
    required: true,
    email: true,
    type: "POST",
    remote: "remotemail.php"
   },
   dateformat: "required",
  ...
</script> 

And now the PHP Code:

<?php
    include('dbsettings.php');

    $conn = mysql_connect($dbhost,$dbuser,$dbpw); 
    mysql_select_db($dbdb,$conn);

    $auslesen1 = "SELECT Email FROM flo_user"; 
    $auslesen2 = mysql_query($auslesen1,$conn); 
    $registered_email = mysql_fetch_assoc($auslesen2);
    $requested_email  = $_POST['Email'];

    if( in_array($requested_email, $registered_email) ){
        echo "false";
    }
    else{
        echo "true";
    }
?>

I tried return TRUE/ return FALSE as well, but this displays "Email is registrated" all the time. json_encode didn't work as well.

Thanks a lot!

A: 

Modify remotemail.php to return false no matter what email address is used. See if you get this to work (i.e. fail validation). Secondly, your PHP code for checking emails looks in-correct... it only checks the first email address in the table and will probably return true for all email addresses. It should be re-written as follows:

// do not forget to add addslashes in the line below, if necessary
$auslesen1 = "SELECT 1 FROM flo_user WHERE Email = '" . $_POST['Email'] . "'"; 
$auslesen2 = mysql_query($auslesen1, $conn); 
$auslesen3 = mysql_fetch_assoc($auslesen2);

echo $auslesen3 === false
    ? "true"   // email does not exist
    : "false"; // email exists
Salman A
Instead of that `SELECT 1` it would be much cleaner to `SELECT COUNT(*)`.
ThiefMaster
I am checking for the presence or absence of record in the next lines of PHP code. As an alternate, yes, I could have used `COUNT(*) AS Foo` and then check `$auslesen3['Foo'] == '0'`.
Salman A