新手需要知道如何修复破碎的PHP电子邮件表单

时间:2016-05-11 06:36:47

标签: php forms email

我认为这很容易(愚蠢,我知道)所以我按照我在本网站上找到的PHP电子邮件表格的说明:http://www.html-form-guide.com/email-form/dreamweaver-email-form.html我根本不需要Dreamweaver界面的帮助,我只需要脚本,我知道(或者我认为我知道)如何使他们的简单形式适应我需要的形式,并相应地调整脚本。

相关表单可在以下网址找到:http://nineinchbride.com/SuitedForWar_BookTwo_PreOrderForm.php

页面上目前存在的代码如下:

<form id="PreOrder_Book_2" name="PreOrder_Book_2" method="post" action="">
          <p>Your Name:
            <input type="text" name="CustomerName" id="CustomerName" />
          </p>
          <p>Your Email:&nbsp;
            <input type="text" name="CustomerEmail" id="CustomerEmail" />
          </p>
          <p>
            <input type="checkbox" name="NotifyPaperback" id="NotifyPaperback" />
          Notify me when paperback is available.</p>
          <p>
            <input type="checkbox" name="Notify_eBook" id="Notify_eBook" />
          Notify me when eBook is available.</p>
          <p>Desired eBook Format:</p>
          <p>
            <label>
              <input type="radio" name="eBookFormats" value=".mobi" id="eBookFormats_0" />
              .mobi (Kindle)</label>
            <br />
            <label>
              <input type="radio" name="eBookFormats" value=".epub" id="eBookFormats_1" />
              .epub (Nook / Ipad / Sony / Kobo)</label>
            <br />
            <label>
              <input type="radio" name="eBookFormats" value=".pdf" id="eBookFormats_2" />
              .pdf (All readers)</label>
            </p>
          <p>
            <input type="submit" name="button" id="button" value="Submit" />
            <br />
          </p>
        </form><script>
        function validateForm()
{
    var name=document.forms["PreOrder_Book_2"]["CustomerName"].value;
    if (name==null || name=="")
    {
        alert("Name cannot be left blank.");
        return false;
    }

    var z=document.forms["PreOrder_Book_2"]["CustomerEmail"].value;
    if (z==null || z=="")
      {
      alert("Please enter your email.");
      return false;
      }
}

</script>
<script><?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['CustomerName'];
$visitor_email = $_POST['CustomerEmail'];
$message = $_POST['NotifyPaperback'];
$message = $_POST['Notify_eBook'];
$message = $_POST['eBookFormats'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

$email_from = 'webmanager@nineinchbride.com';//<== Put your email address here
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "email address: $visitor_email\n".
    "Here is the message:\n $message".

$to = "webmanager@nineinchbride.com";//<== Put your email address here
$headers = "From: $email_from \r\n";

//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: http://nineinchbride.com');

?></script>

请记住,虽然我对代码有所了解(足以适应一些事情,调整命名以保持一致性等),但我不是程序员本身,所以请放轻松我。

更新1:

好的,我在这里取得进展。我对PHP进行了以下更改:

$name = $_POST['CustomerName'];
$visitor_email = $_POST['CustomerEmail'];
$message1 = $_POST['NotifyPaperback'];
$message2 = $_POST['Notify_eBook'];
$message3 = $_POST['eBookFormats'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

$email_from = 'webmanager@nineinchbride.com';//<== Put your email address here
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "email address: $visitor_email\n".
    "Notify When Paperback Is Available: $message1\n".
    "Notify When eBook Is Available: $message2\n".
    "My eBook Format Is: $message3\n".

而且,欢呼,我正在获取所有表单数据。也为我自己想出来了; - )

但是,验证都没有奏效。成功提交后的重定向也不起作用。知道这是怎么回事?

更新2:

哇,验证问题解决了,谢谢茯苓!我刚刚添加了

<input type="button" name="button" id="button" value="Submit" onclick="return validateForm();"/>

到表单本身代替我之前提交的按钮,现在前端验证工作正常。太好了!

但是现在表单本身不再提交了!我做错了什么?

1 个答案:

答案 0 :(得分:1)

你的第一个错误是

$message = $_POST['NotifyPaperback'];
$message = $_POST['Notify_eBook'];
$message = $_POST['eBookFormats'];

将其更改为

$message = $_POST['NotifyPaperback'];
$message .= $_POST['Notify_eBook'];
$message .= $_POST['eBookFormats'];

不是用于连接的点(。)。

其次你从未调用过验证函数

按此更改按钮

<input type="button" name="submit" id="button" value="Submit"  onclick="return validateForm();"/>

现在表格将被提交。

希望它有所帮助!

如有其他问题,请发布另一个问题。