开发自然语言形式伴侣php的问题

时间:2015-09-28 17:53:41

标签: php html css email webforms

原谅我,我是新手,我尝试过的不同的php根本没有用过。我已经找到了自然语言表单的例子,但是没有使用PHP的工具,我在使用传统表单时熟悉PHP,但是我在将两者放在一起时遇到了问题。

一般表格布局如下:
我的名字是[你的名字] 今天,我[whatchya会做什么?] 我这样做是因为[来吧,为什么?] 重要的是我[你说你做了什么]因为[大图] 在[您的电子邮件]上用我真棒的新声明给我发了一封电子邮件 按钮1(发送电子邮件)
按钮2(将所有文本和输入字段复制到剪贴板 - 现在不那么重要)

按钮1,我想使用硬编码的电子邮件地址向我自己发送副本,并使用他们输入的电子邮件地址向用户发送副本。

现在这有点乱,因为我只是想让它工作......再次,我没有包含PHP,因为在这一点上 - 我很困惑自己,以至于我不知道要包括什么。

<form method="post" action="todayiam.php">
  <div class="naturallanguageform">
    <div class="nlfinner">
      <p class="line">
<span class="copy">My name is </span>
        <span class="inputcontainer"><input class="textinput" name="name" value="" placeholder="[your name]">.</span>
        <span class="copy">Today, I am </span>
        <span class="inputcontainer"><input class="textinput" name="todayiam" value="" placeholder="[whatchya gonna do?]">.</span> 
        <span class="copy">I'm doing this because </span>
        <span class="inputcontainer"><input class="textinput" name="why" value="" placeholder="[c'mon, why?]">.</span>
<span class="copy"> It's important that I </span>
        <span class="inputcontainer"><input class="textinput" name="whatusaid" value="" placeholder="[what you said you'd do]"></span>
        <span class="copy"> because </span>
        <span class="inputcontainer"><input class="textinput" name="because" value="" placeholder="[the big picture]">.</span>
      </p>    
<span class="copy">Shoot me an email with my awesome new declaration at</span>
        <span class="inputcontainer"><input class="textinput" name="email" value="" placeholder="[your email]"></span>

      <p class="line">
        <button class="button">Send to E-mail</button>
      </p>
 <p class="line">
        <button class="button">Copy to post in comments</button>
      </p>
    </div>
  </div>
</form>

非常感谢任何帮助。

更新

<?php
       // from the form
       $name = trim(strip_tags($_POST['name']));
       $email = trim(strip_tags($_POST['email']));
       $message = htmlentities($_POST['todayiam']);

       // set here
       $subject = "Contact form submitted!";
       $to = 'email@gmail.com';

       $body = <<<HTML
$message
HTML;

       $headers = "From: $email\r\n";
       $headers .= "Content-type: text/html\r\n";

       // send the email
       mail($to, $subject, $body, $headers);

       // redirect afterwords, if needed
       header('Location: index.html');
?>

另一次更新

我已更改$ headers =“发件人:$ email \ r \ n”; to $ headers =“From:email@gmail.com”。 “\ r \ n”;从地址设置静态(我的支持电子邮件),电子邮件仍然是从CGI Mailer识别的。我已经确认这不是缓存问题,而且正在使用正确的文件。

<?php 
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
    // We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
    $name = $_POST['name'];
    $todayIam = $_POST['todayiam'];
    $why = $_POST['why'];
    $whatYouSaid = $_POST['whatusaid'];
    $because = $_POST['because'];
    $email = $_POST['email'];

    // We define the variables for using in mail()
    $to  = 'email@gmail.com';
    $to .= ', '.$email; // You wanted them to recieve a copy
    $subject = "Contact form submitted!";

    // You can put a lot more headers, check out the mail() documentation
    $headers = "From: email@gmail.com" . "\r\n" ;
    $headers .= "Content-type: text/html\r\n";

    // Compose a $message from all the variables
    $message = "My name is $name. ";
    $message .= "Today, I am $todayIam.";
    $message .= "I'm doing this because $why.";
    $message .= "It's important that I $whatYouSaid";
    $message .= "because $because.";

    if (mail($to, $subject, $message, $header)) {
        // Mail was successfully sent! Do something here
    }
}
?>

在你发布答案之前,我正在编写这个脚本:

<?php
 // from the form
       $name = trim(strip_tags($_POST['name']));
       $email = trim(strip_tags($_POST['email']));
       $message = htmlentities($_POST['todayiam']);


       // set here
       $subject = "Contact form submitted!";
       $to = 'email@gmail.com';

       $body = <<<HTML
$message
HTML;

       $headers = "From: $email\r\n";
       $headers .= "Content-type: text/html\r\n";

       // send the email
       mail($to, $subject, $body, $headers);

       // redirect afterwords, if needed
       header('Location: index.html');
?>

$ email = trim(strip_tags($ _ POST ['email'])); field正在拉取用户的电子邮件并将其用于发送的邮件,如$ header中所述...所以它工作正常。使用您发布的新脚本,我无法使用我的硬编码电子邮件或用户的电子邮件作为FROM。最初,我真的想要了解脚本之间的差异,为什么一个有效,另一个没有,但现在......我真的只是喜欢我的电子邮件被硬编码为FROM。我稍后会担心这些差异。正如我之前所说,我真的试图让它以多种不同的形式工作......我确信这很简单,我看起来像是PHP的新手。感谢。

1 个答案:

答案 0 :(得分:1)

是的,所以在经过评论的一些讨论之后,我决定发布一个答案,给出一些更具可读性的细节。

您的PHP代码缺少$message中所有字段的组合。这很容易做到,因为您可以在PHP中的字符串中放置一个变量。我会告诉你如何。我还将向您展示如何避免未定义的索引

<?php 
if (isset($_POST['name'], $_POST['todayiam'], $_POST['why'], $_POST['whatusaid'], $_POST['because'], $_POST['email']) {
    // We enter this statement only if all the fields has been properly defined, this to avoid getting undefined index
    $name = $_POST['name'];
    $todayIam = $_POST['todayiam'];
    $why = $_POST['why'];
    $whatYouSaid = $_POST['whatusaid'];
    $because = $_POST['because'];
    $email = $_POST['email'];

    // We define the variables for using in mail()
    $to  = 'email@gmail.com';
    $to .= ', '.$email; // You wanted them to recieve a copy
    $subject = "Contact form submitted!";

    // You can put a lot more headers, check out the mail() documentation
    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";

    // Compose a $message from all the variables
    $message = "My name is $name. ";
    $message .= "Today, I am $todayIam.";
    $message .= "I'm doing this because $why.";
    $message .= "It's important that I $whatYouSaid";
    $message .= "because $because.";

    if (mail($to, $subject, $message, $header)) {
        // Mail was sucsessfullly sent! Do something here
    }
}
?>