Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got a problem coding an email-address validation on a webpage.

The first step, checking if the input is like [email protected] works. But the second check, if the email already exists in the database, does not seem to work at all.

I'm using the jQuery Validation plugin.

This is my code so far:

Extract from register.php:

<form action="addKunde.php" id="contact-form" method ="post" class="form-horizontal">
<div class="control-group">
    <label class="control-label" for="email">E-Mail-Addresse</label>
    <div class="controls">
    <input type="text" class="input-xlarge" name="email" id="email">
    </div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.validate.js"></script>
<script src="script.js"></script>

Extract from script.js:

$(document).ready(function(){
    $('#contact-form').validate({
    rules: {
            email: {
                maxlength: 100,
                required: true,
                email: true,
                remote: {
                    url : "check.php",
                    type : "post"
                }
            }
        },
        highlight: function(element) {
            $(element).closest('.control-group').removeClass('success').addClass('error');
        },
        success: function(element) {
            $(element).addClass('valid').closest('.control-group').removeClass('error').addClass('success');
        }
    });
});

Extract from check.php:

<?php
include('mysql.php');
$email = trim(strtolower($_POST['email']));
$email = mysqli_real_escape_string($con, $username);
$result = mysqli_query($con, "SELECT * FROM Kunde WHERE EMail = '$email' LIMIT 1;");
$num = mysqli_num_rows($result);
if($num == 0){
    echo "true";
} else {
    echo "E-Mail-Adresse schon registriert.";
}
mysqli_close($con);
?>

I've been googling for hours now, following all kinds of different instructions and even trying to implement an own validation method using jQuery-Validation's addMethod.

I hope someone can help me.

Thanks a lot!

share|improve this question
1  
And the nature of this problem would be .... ?? – Pointy 18 hours ago
I'm sorry. I added some description. – user2407408 18 hours ago

2 Answers

I figured it out by myself: The problem was in the check.php. Instead of converting the email-adress sent by post request using

$email = trim(strtolower($_POST['email']));
$email = mysqli_real_escape_string($con, $username);

(I'm don't even remember where i found this piece of code) Anyway, I replaced it with:

$email = urldecode($_POST['email']);

It works. Returning a custom validation message as string is possible, too. It has to be put in escaped quotes so that javascript treats it like a string:

echo "\"eMail-Address already registered.\"";

Thanks for your support.

share|improve this answer

Perhaps the string "E-Mail-Adresse schon registriert." is evaluating to true for the success check. Can you try changing it to "false" and see if it makes a difference?

share|improve this answer
I replaced the String with "false". The result: Every email-address of type [email protected] gets a message: "Please fix this field.". – user2407408 9 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.