about

Freelance Code is a place to find scripts for your site. If you would like to submit your script, just contact us with the name, url, description, cost, and what programming language it should fall under.

PHP Formmail

// PHP Form Mail by Brent Todd.
// Created October 14, 2003
// For updates to this script check out www.brenttodd.com or www.freelancecode.com

function displayForm($name,$email,$subject, $message) {
    // This just echos out the stuff for the form
    echo "<form action=\"\" method=\"post\">";
    echo "Name: <input type=text name=name value=\"$name\"><br>";
    echo "Email: <input type=text name=email value=\"$email\"><br>";
    echo "Subject: <input type=text name=subject value=\"$subject\"><br>";
    echo "Message:<br><textarea rows=5 cols=30 name=message>$message</textarea><br>";
    echo "<input type=submit name=submit value=\"Send Message!\"></form>";
}

if ($_POST[submit]) { // Form was submitted
    // General Config Options
    // each item by default is set to 1 which means it is required
    // setting the item to 0 means it is not required
    $req_name = 1;
    $req_email = 1;
    $req_subj = 1;
    $req_msg = 1;

//check for errors
    $error = 0;
    if ($req_name && !$_POST[name]) {
        $error +=1;
        echo "You forgot to enter your name.<br>";
    }
    if ($req_email && !$_POST[email]) {
        $error +=1;
        echo "You forgot to enter your email.<br>";
    }
    if ($req_subj && !$_POST[subject]) {
        $error +=1;
        echo "You forgot to enter a subject.<br>";
    }
    if ($req_msg && !$_POST[message]) {
        $error+=1;
        echo "You forgot to enter a message.<br>";
    }
    if (!strpos($_POST[email], "@")) {
        //checks for an @ in the email address
        $error +=1;
        echo "Your email address appears to be invalid.<br>";
    }

    // set variables for easier
    $nm = $_POST[name];    
    $em = $_POST[email];
    $sb = $_POST[subject];
    $ms = $_POST[message];

    if ($error) { // there was at least one error
        echo "You had $error error(s).  Please see above messages for what was wrong.";
        displayForm($nm,$em,$sb,$ms);
    }
    else {
        // Remember to enter your email or it will not work
        mail("ENTER YOUR EMAIL", "$sb", "$ms", "FROM: $nm <$em>");
        echo "Message sent.  Thank you for your feedback.";
    }
}
else { //Form has not been submitted, display it.
    echo "If you would like to contact me, please use the form below<br>";
    displayForm("","","","");
}
?>
PHP Formmail can be found in the following categories: PHP  



Last 3 entries