php form validation and error messages

no0ne

I'm setting up this newsletter subscription form to communicate with my database, but before sending the information(email) I just want to validate it. However when I enter wrong or empty data, it displays both error messages. I'm no expert on PHP but it seems I'm missing some closing brackets or something.. I've fiddled around now for a while with no success, so I turn for help to you guys again :)

<?php
$host = "XXX";
$username = "name";
$password = "XXX";
$database = "base";
$connect = @mysql_connect($host, $username, $password) or die (@mysql_error());
$selectdb = @mysql_select_db($database, $connect) or die (@mysql_error());


if(isset($_POST['submit'])){
$email = $_POST['email'];
if(empty($email)){
    echo "Hmm, you did not anything..";
    }
    if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
    $email_address))
    {
    echo "This email not valid";
    }
    else{
    @mysql_query("INSERT INTO newsletters SET email='$email'");
    echo "Thanks you";
}

}
?>

Thank you!!

Amal Murali

You're currently using the following structure:

if (condition) {

}
if (condition) {

}
else (condition) {

}

Change the structure to:

if (condition) {

}
elseif (condition) {

}
else (condition) {

}

That way, the elseif statement will get executed ONLY if the first if statement evaluates to FALSE.

Also, in your preg_match statement, you're using the variable $email_address which isn't defined anywhere, as far as I can see.

Change it to $email:

if (!preg_match(
    "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
    $email))

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extend contact form validation PHP script with some error messages

From Dev

Symfony 2.6 form validation and error messages

From Dev

Laravel 5.0 separated error messages for form validation

From Dev

[Spring MVC - Thymeleaf]- Form validation and error messages

From Dev

Zend Form Validation set error messages

From Dev

Symfony 2.6 form validation and error messages

From Dev

CFWheels: Form validation customized error messages

From Dev

Codeigniter form validation error messages with bootstrap

From Dev

Form validation error messages not appearing as expected

From Dev

Validation error messages not showing in form view

From Dev

Translating form validation messages

From Dev

Laravel 5.1 How to output HTML in form validation error messages

From Dev

Why does simple form not show validation error messages in Rails?

From Dev

Rails form validation not show the error messages, taking blank fields

From Dev

Jquery - form validation, add css styles to the error messages

From Dev

Showing all error messages at the same time in form validation

From Dev

Custom form error validation in Angular-material ng-messages

From Dev

CodeIgniter Form Validation matches got too many error messages

From Dev

Contact form phone number validation error PHP

From Dev

Rails displaying validation messages on form

From Dev

Showing form error messages

From Dev

Formatting form error messages

From Dev

Bootstrap Validation Error Messages CSS

From Dev

Implementing data validation with error messages

From Dev

Simple_Form display validation error messages next to different input field

From Dev

PHP form is not capturing message due to unkown validation error

From Dev

Display form validation error message on same page using only PHP?

From Dev

Parsley Form Validation Error

From Dev

Form validation Jquery no error

Related Related

  1. 1

    Extend contact form validation PHP script with some error messages

  2. 2

    Symfony 2.6 form validation and error messages

  3. 3

    Laravel 5.0 separated error messages for form validation

  4. 4

    [Spring MVC - Thymeleaf]- Form validation and error messages

  5. 5

    Zend Form Validation set error messages

  6. 6

    Symfony 2.6 form validation and error messages

  7. 7

    CFWheels: Form validation customized error messages

  8. 8

    Codeigniter form validation error messages with bootstrap

  9. 9

    Form validation error messages not appearing as expected

  10. 10

    Validation error messages not showing in form view

  11. 11

    Translating form validation messages

  12. 12

    Laravel 5.1 How to output HTML in form validation error messages

  13. 13

    Why does simple form not show validation error messages in Rails?

  14. 14

    Rails form validation not show the error messages, taking blank fields

  15. 15

    Jquery - form validation, add css styles to the error messages

  16. 16

    Showing all error messages at the same time in form validation

  17. 17

    Custom form error validation in Angular-material ng-messages

  18. 18

    CodeIgniter Form Validation matches got too many error messages

  19. 19

    Contact form phone number validation error PHP

  20. 20

    Rails displaying validation messages on form

  21. 21

    Showing form error messages

  22. 22

    Formatting form error messages

  23. 23

    Bootstrap Validation Error Messages CSS

  24. 24

    Implementing data validation with error messages

  25. 25

    Simple_Form display validation error messages next to different input field

  26. 26

    PHP form is not capturing message due to unkown validation error

  27. 27

    Display form validation error message on same page using only PHP?

  28. 28

    Parsley Form Validation Error

  29. 29

    Form validation Jquery no error

HotTag

Archive