简体   繁体   English

添加额外的INPUT以联系表单电子邮件PHP

[英]Add extra INPUT to contact form email PHP

I'm using some php i found for sending a contact form from my site. 我正在使用一些php从我的网站发送联系表格。

The HTML looks like this: HTML看起来像这样:

<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看起来像这样:

<?php

define("WEBMASTER_EMAIL", 'your@emailadress.com');

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. 我知道如何将这些添加到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? 但是我只是找不到将输入内容添加到我在邮箱中收到的电子邮件中的好方法...在PHP中的何处添加此输入? I tried lots of things... 我尝试了很多事情

Hope you guys can help me out! 希望你们能帮助我!

David 大卫

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: 最后,您使用变量lastname将其显示在电子邮件中:

"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 : 编辑:如果要在消息上使用此输入,则可以使用$message变量,并且可以这样做:

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

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. 如果您不希望它作为“发件人”或“主题”的附加内容出现在电子邮件标题中,则还可以继续将信息附加到电子邮件正文或$ 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. 其他选项也起作用,它实际上仅取决于您希望这些额外信息进入电子邮件的“位置”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM