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


Newbie Needs To Know How To Fix A Broken PHP Email Form

我觉得这会很容易(我知道这很傻),所以我按照我在这个网站上找到的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:

哇,验证问题解决了,谢谢你Poria!我刚刚添加了

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

到表单本身,而不是我以前的提交按钮,现在前端验证正在工作。太棒了

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

您的第一个错误是

$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();"/>

现在将提交表格。

希望它能有所帮助!

如需更多问题,请发布另一个问题。