将字段添加到contact.php

时间:2011-02-07 18:45:44

标签: php email templates webforms

我在线获得了一个模板...他们包含了这个contact.php - 我如何添加额外的字段,以便这个contact.php将它发送给我。

<?php

if(!$_POST) exit;

$email = $_POST['email'];


//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}
if($errors==1) echo $error;
else{
    $values = array ('name','email','message');
    $required = array('name','email','message');

    $your_email = "myemail@live.ca";
    $email_subject = "New Message: ".$_POST['subject'];
    $email_content = "new message:\n";

    foreach($values as $key => $value){
      if(in_array($value,$required)){
        if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
      }
    }

    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Message sent!'; 
    } else {
        echo 'ERROR!';
    }
}
?>

HTML联系页面是:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function(){
    $('#contactform').submit(function(){                  
        var action = $(this).attr('action');
        $.post(action, { 
            name: $('#name').val(),
            email: $('#email').val(),
            telephone: $('#telephone').val(),
            company: $('#company').val(),
            subject: $('#subject').val(),
            message: $('#message').val()
        },
            function(data){
                $('#contactform #submit').attr('disabled','');
                $('.response').remove();
                $('#contactform').before('<p class="response">'+data+'</p>');
                $('.response').slideDown();
                if(data=='Message sent!') $('#contactform').slideUp();
            }
        ); 
        return false;
    });
});
// ]]>
</script>
</head>
<body>
<div class="main">
  <div class="blok_header">
    <div class="header">
      <div class="search">
      </div>
      <div class="clr"></div>
      <div class="withe_bg">
        <div class="logo"></div>
        <div class="menu">        </div>
        <div class="clr"></div>
        <div class="body">
        <h3>&nbsp;</h3>
        <p>&nbsp;</p>

        <div class="left">
          <h2>Send us a Message:</h2>
          <p>Please use this convenient form to your send your message, and we will get back to you shortly.</p>
          <form action="contact.php" method="post" id="contactform">
            <ol>
              <li>
                <label for="name">Name <span class="red">*</span></label>
                <input id="name" name="name" class="text" />
              </li>
              <li>
                <label for="telephone">Telephone <span class="red"></span></label>
                <input id="telephone" name="telephone" class="text" />
              </li>
              <li>              
                <label for="email">Your email <span class="red">*</span></label>
                <input id="email" name="email" class="text" />
              </li>
              <li>
                <label for="company">Company</label>
                <input id="company" name="company" class="text" />
              </li>
              <li>
                <label for="subject">Subject</label>
                <input id="subject" name="subject" class="text" />
              </li>
              <li>
                <label for="message">Message <span class="red">*</span></label>
                <textarea id="message" name="message" rows="6" cols="50"></textarea>
              </li>
              <li class="buttons">
                <input type="image" name="imageField" id="imageField" src="images/send.gif" class="send" />
                <div class="clr"></div>
              </li>
          </ol>
        </form>
        </div>
        <div class="right last">

           <p>&nbsp;</p>
           <p>
           </p>

           </div>

          <div class="clr"></div>
        </div>
        <div class="clr"></div>
      </div>
      <div class="clr"></div>
    </div>
  </div>
  <div class="clr"></div>
</div>
<div class="FBG">
  <div class="FBG_resize">
  <div class="clr"></div>
      </div>
      <div class="clr"></div>
    </div>
  </div>
  <div class="clr"></div>
</div>
<div class="FBG">
  <div class="FBG_resize">
    <div class="clr"></div>
  </div>
  <div class="clr"></div>
</div>
<div class="footer">
  <div class="footer_resize">
    <p class="leftt">&nbsp;</p>
    <div class="clr"></div>
  </div>
  <div class="clr"></div>
</div>
</body>
</html>

仍然无效......

4 个答案:

答案 0 :(得分:0)

您的意思是您也希望将联系邮件发送给您,对吧?如果是这样,请尝试以下代码。 mail是完成这个技巧的php邮件功能。有关更多参考,请查看MANUAL

<?php
$email = $_POST['email'];

//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
    $error.="Invalid email address entered";
    $errors=1;
}
if($errors==1) echo $error;
else{
    $values = array ('name','email','message');
    $required = array('name','email','message');

    $your_email = "seanbridge@live.ca";
    $email_subject = "New Message: ".$_POST['subject'];
    $email_content = "new message:\n";

    foreach($values as $key => $value){
      if(in_array($value,$required)){
        if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; }
        }
        $email_content .= $value.': '.$_POST[$value]."\n";
      }
    }

    if(@mail($your_email,$email_subject,$email_content)) {
        echo 'Message sent!'; 
    } else {
        echo 'ERROR!';
    }

    $myEmail = "myemai@your.com";//put your email address here

    if(@mail($myEmail,$email_subject,$email_content)) {
        echo 'Message sent!'; 
    } else {
        echo 'ERROR!';
    }
}
?>

答案 1 :(得分:0)

这不是表单本身,而是表单处理器。你可能在表格的某个地方有一个HTML文件,发布到这个contact.php文件。

无论如何,看起来你必须将你在表单中请求的字段数与$ values数组中的#项(具有正确的名称)相匹配。看起来你可以在$ required数组中要求某些。

将$ your_email更改为您的电子邮件地址。

您应该通过表单收到电子邮件。

这会回答你的问题吗?这不是特别具体。

答案 2 :(得分:0)

如果您希望通过电子邮件发送给多个人,则应该查看参数部分(“收件人”部分)here。这将在你的

$your_email = "myemail@live.ca";

以下是一个例子:

$your_email = "<user@example.com>, Another User <anotheruser@example.com>";

应将其发送至user@example.com和anotheruser@example.com

答案 3 :(得分:0)

修复方法是:

编辑:$ required = array('name','email','telephone','message');

它有效...... 100% - 谢谢所有......