How can I set up a PHP form to validate content before deciding to send as an email

3 replies
'm a web designer, and the site I am making wants many different forms.
So far, I have used PHP sanitizing and logical filters to validate the input, but how can I program the form to abort sending the email if the content isn't validated?
#content #deciding #email #form #php #send #set #validate
  • Profile picture of the author Speedyapoc
    What I like to do is have an integer based "error" variable that starts at 0. In the event that something does not get validated, I change that variable to a 0. If the error variable equals 0 after the validation, I know that everything is valid and I can send mail.

    For some reason, WF isn't allowing me to place this in [code ] or [php ] tags.

    function checkName($string) {
    return preg_match('my regexp here', $string);
    }

    function checkPhone($string) {
    return preg_match('my regexp here', $string);
    }

    function checkEmail($string) {
    return preg_match('my regexp here', $string);
    }

    if (isset($_POST['name']) &&
    isset($_POST['phone']) &&
    isset($_POST['email'])) {

    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];

    $error = 0;

    if (!checkName($name) ||
    !checkPhone($phone) ||
    !checkEmail($email)) $error = 1;

    if ($error == 0) {
    mail('you@example.com','bla','bla','bla');
    die ('Thank-you for your feedback.');
    } else {
    die('An error has occurred. Make sure you entered valid information.');
    }

    } else {

    die('Please fill in all fields.');

    }
    {{ DiscussionBoard.errors[4425810].message }}
  • Profile picture of the author ADHardwick
    Or you could just have the PHP call up an HTML form that validates.
    Signature
    Homepage Theme for Wordpress
    Rock Your Adsense Earnings with Custom Google Search Skins!
    {{ DiscussionBoard.errors[4428867].message }}
  • Profile picture of the author ADHardwick
    And I am thinking that maaaaybe you could have an HTML form that validates via variables defined elsewhere
    Signature
    Homepage Theme for Wordpress
    Rock Your Adsense Earnings with Custom Google Search Skins!
    {{ DiscussionBoard.errors[4428874].message }}

Trending Topics