0

My php code is not working. It is showing wrong messages. For example if I fill the email address longer than 10 characters, then it still shows that my field is empty.

<?php
    $errors = array();

    if (isset($_POST['submit'])) {
        $name  = htmlentities(mysql_real_escape_string($_POST['name']));
        $email = htmlentities(mysql_real_escape_string($_POST['email']));
        $ip    = $_SERVER['REMOTE_ADDR'];

        if (strlen($email) > 10) {
            echo 'ok';  
        } 
        else {
            echo 'empty';
        }
    }
?>
3
  • Check your html form. There might be a problem there. Plus. You are doing everything wrong as far as input sanitation goes. In simple terms, Don't use any function that starts with mysql_, learn/use mysql or PDO Commented Aug 9, 2014 at 15:19
  • <input type="text" name="name" id="name" class="detail" autocapitalize="words" autocorrect="off"> <input type="text" id="f1" class="detail" name="email" autocapitalize="words" autocorrect="off" /> <input type="submit" value="Continue" class="csssubmit" name="submit"> everything is inside a form.. Commented Aug 9, 2014 at 15:21
  • do var_dump($_POST['email']); before the $name Commented Aug 9, 2014 at 15:26

1 Answer 1

3

if you want to validate if the email address is 10 or under 10 -> error then u should use

if(strlen($email)<=10) {
echo 'ok';  
} else {
echo 'empty';
}

but there are other email validators that are better

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // invalid emailaddress
}

because what is my email is [email protected] then it will validate false but if thats my email adress then i have a problem

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

3 Comments

it should show ok if the email is 10 or under 10.. if its over 10 it should echo empty... pls do print_r($_POST); before name
pls show me the print_r results then i can say more
No problem.. check best practise for this because the overal of you code is not that proper.....

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.