Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Email

  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Email

    I am new to php and am trying to set up a contact form correctly -

    This is my form:

    <form action="#" id="contactForm">
    <div class="holder">
    <label>Name</label>
    <input type="text" value="" name="name" class="textbox" />
    </div>

    <div class="holder">
    <label>E-Mail</label>
    <input type="text" value="" name="email" class="textbox" />
    </div>

    <div class="holder">
    <label>Subject</label>
    <input type="text" value="" name="subject" class="textbox" />
    </div>

    <div class="holder">
    <label>Message</label>
    <textarea cols="25" rows="5" name="message" class="textbox"></textarea>
    </div>

    <div class="holder">
    <input type="submit" value="Send Message" name="submit" class="submit" />
    </div>
    </form>
    </div>


    this is emailform.php:

    <?php
    if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "info@creativecollective.biz";
    $email_subject = "New e-mail subscriber";


    function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form your submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
    }

    // validation expected data exists
    if
    (!isset($_POST['email'])) {
    died('We are sorry, but there appears to be a problem with the email your submitted.');
    }


    $email_from = $_POST['email']; // required

    $error_message = "";
    $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
    if(!eregi($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
    }

    if(strlen($error_message) > 0) {
    died($error_message);
    }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
    }


    $email_message .= "Email: ".clean_string($email_from)."\n";


    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
    ?>

    <!-- include your own success html here -->

    Thank you for contacting us. We will be in touch with you very soon.

    <?
    }
    ?>


    this is my functions.php

    <?php
    function ValidateEmail($email)
    {
    /*
    (Name) Letters, Numbers, Dots, Hyphens and Underscores
    (@ sign)
    (Domain) (with possible subdomain(s) ).
    Contains only letters, numbers, dots and hyphens (up to 255 characters)
    (. sign)
    (Extension) Letters only (up to 10 (can be increased in the future) characters)
    */

    $regex = '/([a-z0-9_.-]+)'. # name

    '@'. # at

    '([a-z0-9.-]+){2,255}'. # domain & possibly subdomains

    '.'. # period

    '([a-z]+){2,10}/i'; # domain extension

    if($email == '') {
    return false;
    }
    else {
    $eregi = preg_replace($regex, '', $email);
    }

    return empty($eregi) ? true : false;
    }
    ?>

    this is my contact-action.php

    <?php
    /*
    Credits: Bit Repository
    URL: http://www.bitrepository.com/
    */

    include 'config.php';

    error_reporting (E_ALL ^ E_NOTICE);

    $post = (!empty($_POST)) ? true : false;

    if($post)
    {
    include 'functions.php';

    $name = stripslashes($_POST['name']);
    $email = trim($_POST['email']);
    $subject = stripslashes($_POST['subject']);
    $message = stripslashes($_POST['message']);


    $error = '';

    // Check name

    if(!$name)
    {
    $error .= 'Please enter your name.<br />';
    }

    // Check email

    if(!$email)
    {
    $error .= 'Please enter an e-mail address.<br />';
    }

    if($email && !ValidateEmail($email))
    {
    $error .= 'Please enter a valid e-mail address.<br />';
    }

    // Check message (length)

    if(!$message || strlen($message) < 15)
    {
    $error .= "Please enter your message. It should have at least 15 characters.<br />";
    }


    if(!$error)
    {
    $mail = mail(WEBMASTER_EMAIL, $subject, $message,
    "From: ".$name." <".$email.">\r\n"
    ."Reply-To: ".$email."\r\n"
    ."X-Mailer: PHP/" . phpversion());


    if($mail)
    {
    echo 'OK';
    }

    }
    else
    {
    echo '<div class="notification_error">'.$error.'</div>';
    }

    }
    ?>

    and finally my config.php

    <?php
    // To
    define("WEBMASTER_EMAIL", 'info@creativecollective.biz');
    ?>

    But my form doesn't work. What am I doing wrong?

    Thank you

  • #2
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could try for a start looking at the HTML form tag in your form:

    Code:
    <form action="#" id="contactForm">
    If you dont specify the method the form will use GET by default - and you will see all the form elements listed in the querystring once the form is submitted. Which in turn populates the $_GET array; and not the $_POST array which is what you seem to be wanting to check.

    Change it to this:

    Code:
    <form action="#" id="contactForm" method="post">
    Now the $_POST array will be populated and the form should work as expected.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •