SpaceApe
SpaceApe

Reputation: 639

Add extra INPUT to contact form email PHP

I'm using some php i found for sending a contact form from my site.

The HTML looks like this:

<div id="wrap">
    <div id='form_wrap'>
        <form id="contact-form" action="javascript:alert('success!');">

    <p id="formstatus"></p>
            <input type="text" name="name" value="" id="name" placeholder="Voornaam" required/>
            <input type="text" name="email" value="" id="email" placeholder="E-mail adres" required/>
            <textarea  name="message" value="Your Message" id="message" placeholder="Uw vraag of projectomschrijving" required></textarea>
            <input type="submit" name ="submit" value="Offerte aanvragen" />

        </form>
    </div>
</div>

The PHP looks like this:

<?php

define("WEBMASTER_EMAIL", '[email protected]');

error_reporting (E_ALL ^ E_NOTICE);

function ValidateEmail($email)
{
    $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;
}

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

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

    $error = '';

    // Check name
    if(!$name || $name == "Name*")
        $error .= 'Please enter your name.<br />';

    // Check email
    if(!$email || $email == "Email*")
        $error .= 'Please enter an e-mail address.<br />';

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

    // Check message
    if(!$message)
        $error .= "Please enter your message. <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="formstatuserror">'.$error.'</div>';
}?>

It works great! BUT i need to add a few more INPUTS in my form. I know how to add those to the html. For instance I added this one:

<input type="text" name="lastname" value="" id="lastname" placeholder="Achternaam" required/>

But I just can't find the good way to add this input to the email that I receive in my mailbox... Where do I add this in the PHP? I tried lots of things...

Hope you guys can help me out!

David

Upvotes: 0

Views: 101

Answers (2)

Akram Fares
Akram Fares

Reputation: 1683

After these lines:

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

You instanciate a variable containing the value for your field (lastname):

    $lastname = stripslashes($_POST['lastname']);

Then you validate your input if it's empty or something else:

 // Check message
    if(!$lastname )
        $error .= "Please enter your lastname. <br />";

And finally, you use your variable lastname to display it on the email message:

"From: ".$name." ".$lasname." <".$email.">\r\n"

Et voilà !

EDIT: If you want to use this input on the message, you have the $message variable, and you can do so :

if(!$error)
    {    
         $message .= "<p>Message sent by $lastname";
...

Upvotes: 1

TheLords010
TheLords010

Reputation: 11

If you didn't want it going in the email headers as an addition to the 'From' or 'Subject', you could also keep appending information to the body of the email, or your $message. For example:

$name    = stripslashes($_POST['name']);
$email   = trim($_POST['email']);
$subject = stripslashes($_POST['subject']);
$message = "Last Name: " . stripslashes($_POST['lastname']) . "\r\n";
$message .= "Message: " . stripslashes($_POST['message']);

The other options work as well, it really just depends 'where' in the email you want this extra information going.

Upvotes: 0

Related Questions