0

I try to display an error message when email is used with jQuery validation. Unfortunately there is no error message, when the email is already used. I tried the following:

Javascript

$('#registerForm').validate({
    rules: {
        registerEmail: {
            required: true,
            remote: 'ajax/ajax.validateEmail.php'
        }
    },
    messages: {
        registerEmail: {
           remote: 'E-Mail already taken'
        }
    }
});

PHP

<?php
header('Content-type: application/json');

$stmt = $mysqli->prepare("SELECT * FROM members m WHERE mEmail = ? LIMIT 1");
$stmt->bind_param('s', $_POST['registerEmail']);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();

if ($stmt->num_rows == 1) {
    echo false;
}else{
    echo true;
}

What am I doing wrong here? Thanks in advance for your help!

Sparky
  • 98,165
  • 25
  • 199
  • 285
cr1zz
  • 577
  • 1
  • 5
  • 16

3 Answers3

0

You need echo return true or false in the backend php file.

Instead of

echo(json_encode(false));

change

echo 'false';

Same for TRUE

Example

Explanation:

jQuery remote method required an output either true or false from remote url.

If we echo true there, jQuery consider it as boolen true

and if we echo false, jQuery will convert it to boolean false.

Pupil
  • 23,834
  • 6
  • 44
  • 66
  • Hey thanks. I did it without apostrophes (echo true;), cause with nothing happens. But now on every onBlur the email field gets red with a validation message '1' ?! – cr1zz Dec 16 '14 at 12:31
0

And there is a syntax error:

$('#registerForm').validate({
    rules: {
        registerEmail: {
        required: true,
        remote: 'ajax/ajax.validateEmail.php'
    },

should

$('#registerForm').validate({
    rules: {
        registerEmail: {
            required: true,
            remote: 'ajax/ajax.validateEmail.php'
        }
    },
fechnert
  • 1,215
  • 2
  • 12
  • 30
0

Ok, find the trick:

  • Edited Javascript line to: remote: { url: 'ajax/ajax.validateEmail.php', type:'post', async:false }
  • Removed: header('Content-type: application/json');
  • Edited return line to: echo json_encode( false );

Hope this will help anyone too :)

cr1zz
  • 577
  • 1
  • 5
  • 16