i have the following php script. the databtase table i'm saving the data to allows NULL values for the email. this is the table structure.

`

CREATE TABLE tblcontacts (
    contactID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    contactName VARCHAR( 50  ) NOT NULL,
    phoneNumber VARCHAR( 20 ) UNIQUE NOT NULL,
    emailAddress VARCHAR( 60 )
) ;

and this is the php script.

<?php

    /**
    * @author FreeUser
    * @copyright 2013
    */
    function is_digits($element) {
        return !preg_match ("/[^0-9]/", $element);
    }

    if ( isset( $_POST['submitted'] ) ) {
        $errors = array();

        if ( is_digits( $_POST['txtPhoneNumber'] ) ) {
            $errors[] = $_POST['txtPhoneNumber'];
        } else {
            $errors[] = 'Phone number must have numeric values only.';
        }

        if ( isset( $_POST['txtEmail'] ) ) {
            if ( filter_var( $_POST['txtEmail'], FILTER_VALIDATE_EMAIL ) ) {
                $errors[] = $_POST['txtEmail'];
            } else {
                $errors[] = 'Please enter a valid email.';
            }
        }elseif ( empty( $_POST['txtEmail'] ) ) {
            $email = '';
        }

        if ( preg_match( '/^[A-Z \']{10,50}$/i', $_POST['txtContactName'] ) ) {
            $contact = ucwords( $_POST['txtContactName'] );
        } else {
            $errors[] = "The branch contact must contain letters and spaces only.";     
        } 
    }

?>

<html>
    <head><title>Add Phone Number</title>
    </head>
    <body>
        <?php 
            if ( isset( $errors) && is_array( $errors ) ) {
                foreach( $errors as $error ) {
                    echo '- ' . $error .  '<br />';
                }
            }
        ?>
        <form method="post" action="">
            <p>Contact Name: <input type="text" name="txtContactName" value="<?php if ( isset( $_POST['txtContactName'] ) ) echo $_POST['txtContactName']; ?>" /></p>
            <p>Telephone Number: <input type="text" name="txtPhoneNumber" value="<?php if ( isset( $_POST['txtPhoneNumber'] ) ) echo $_POST['txtPhoneNumber']; ?>" /></p>
            <p>Email Address: <input type="text" name="txtEmail" value="<?php if ( isset( $_POST['txtEmail'] ) ) echo $_POST['txtEmail']; ?>"/></p>
            <p><input type="submit" value="Add Contact"/><input type="hidden" name="submitted" /></p>
        </form>
    </body>
</html>

i want it to validate the email only if a value has been supplied. `

i want it to validate the email only if a value has been supplied

You do this already in PHP. What exactly is the problem?

if i don't supply a value for the email field, the form is not submitted

Change lines 20-28 to:

$email = '';
if (isset($_POST['txtEmail']) and !empty($_POST['txtEmail'])) 
{
    if (filter_var($_POST['txtEmail'], FILTER_VALIDATE_EMAIL)) 
    {
        $email = $_POST['txtEmail'];
    }
    else
    {
        $errors[] = 'Please enter a valid email.';
    } 
}

thanks, it's now working like a charm

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.