为什么这个 php 代码不起作用,我如何编码确认两封电子邮件是相同的


Why won't this php code work and how do I code a confirmation that both emails are the same?

我正在尝试为我正在构建的网站制作表单。我对编码很陌生,但我以前做一些简单的网站,并且对网络编码有非常基本的了解。我有两个问题。1(我正在尝试对此表单进行编码,以便如果两个电子邮件字段不匹配,将以某种方式显示一条消息通知查看者。2(发送按钮将通过电子邮件发送信息,但有两个部分不起作用:复选框数组不起作用(它只说"数组",我希望它说用逗号分隔的值(,我不知道如何让它重定向到单独的成功页面(即contact_success.html(。单独给我答案很好,但如果你能告诉我你为什么使用特定的代码,那就太好了。

以下是整个表单的 HTML 代码:

<form name="contact" action="contactProcess.php" method="post" onSubmit="return validateForm()">
<table  width="100%" cellspacing="6">
    <tr>
        <th width="30%" class="th_contact">*Name:</th>
        <td width="70%"><input type="text" size="50" name="name"></td>
    </tr>
    <tr>
       <th class="th_contact">*Your email  address:</th>
       <td><input type="text" size="50" name="email"></td>
   </tr>
   <tr>
       <th class="th_contact">*Confirm email  address:</th>
       <td><input type="text" size="50" name="email2"></td>
   </tr>
   <tr>
       <th class="th_contact">How did you hear about us?</th>
       <td><input type="radio" name="referral" value="friend or family">Friend or Family<br />
       <input type="radio" name="referral" value="veterinarian">Veterinarian<br />
       <input type="radio" name="referral" value="flyer or mailer">Flyer or Mailer<br />
       <input type="radio" name="referral" value="humane society or similar facility">Humane     Society or Similar Facility<br />
       <input type="radio" name="referral" value="walked or drove by">Walked or Drove By<br />
       <input type="radio" name="referral" value="other">Other<br />
       </td>
   </tr>
   <tr>
       <th valign="top"><h5>What type of pet do you have?</h5></th>
       <td><input type="checkbox" name="pet[]" value="dog">Dog<br />
       <input type="checkbox" name="pet[]" value="cat">Cat<br />
       <input type="checkbox" name="pet[]" value="bird">Bird<br />
       <input type="checkbox" name="pet[]" value="fish">Fish<br />
       <input type="checkbox" name="pet[]" value="reptile">Reptile<br />
       <input type="checkbox" name="pet[]" value="small animal">Small Animal<br />
       </td>
   </tr>
       <td  align="left"><input type="submit"  class="button" value="submit">
       *Required  Fields</td>
   </tr>
</table>
</form>

这是 PHP:

<!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>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>Contact PHP</title>
     <link href="A4P.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php {
//Set up message body
$message = "Form Submitted on: " . date("m/d/y") . "'n";
$message .= "'n";
$message .= "Name: " . $_POST['name'] . "'n";
$message .= "Email Address: " . $_POST['email'] . "'n'n";
$message .= "Referred by: " . stripslashes($_POST['referral']) . "'n";
$message .= "Pets: " . $_POST['pet'];

//Set up email
$to = "example@example.com, <" . $_POST['email'] . ">";
$subject = "New Email Club Entry";
$headers = "From: " . "All 4 Pets Website";
//Send email
mail($to,$subject,$message,$headers);
}
?>
</body>
</html>

以下是电子邮件的外观,以防万一:

表格提交于: 11/18/13

姓名:布拉德·摩尔
电子邮件地址: 123@yahoo.com

转介人:步行或驾车
宠物:数组

  1. if ($_POST['email'] != $_POST['email2'])
    {
        //alert user
    }
    
  2. 不能像字符串一样转储数组。我相信复选框的 HTML 应该是 name="pets" 而不是 "pets[]"。

    $str = implode(" , " , $_POST['pets']);
        //now it's a string
    

你要找的是验证你的代码。此链接将提供验证代码所需的详细信息,您是选择在同一页面上验证还是在其他页面上验证取决于您。如果您对此答案有更多疑问,请对此答案发表评论。

http://www.w3schools.com/php/php_form_validation.asp

在同一页面上验证意味着您需要检查是否首先按下了提交按钮,即

if(isset($_POST['button'])) {
    //your code here
}

然后,您要检查帖子中的电子邮件是否相同:

if(isset($_POST['button'])) {
    if($_POST['email1'] != $_POST['email2']{
        //do yourself a favor and use an array to store the errors in
    }
    else{
        //you can choose to let the user know that it was a success or nothing at all.
    }
}

验证您的 PHP 代码允许您检查以确保用户正在执行您最终希望他们执行的操作。如果您不验证,从技术上讲,该提交按钮正在"发布",但除了之后要求的功能之外,并没有真正做任何事情。将验证代码放在一组新标记中的标记上方,或者直接放在您已经编写的所有代码的下方。除此之外,如果您在同一页面上进行验证,您希望将代码更改为如下所示:

<form action="" method="post">

这样,它就知道在同一页面上进行验证,并且您不会选择在不同的页面上进行验证。

要检查两个变量是否匹配,只需使用 if 语句:

if($_POST['email1']==$_POST['email2']) {
    //emails match, continue with form submission
} else {
    //emails do not match, show error
}

要显示选中了哪些复选框,请使用此代码将值放入单个字符串中。

$checked = implode(" , ", $_POST['pets']);
//$checked would be values of selected checkboxes, each separated by a comma

要在成功提交后重定向到页面,请使用以下代码:

header("Location: success.html");

您应该进行验证并清理您的输入,但这是您要求的。 :)

1(由于您的表单中已经有一个validateForm()调用,我假设您想使用 JavaScript 检查邮件地址。

首先,您应该为此更改表单:

onsubmit="return validateForm(email.value, email2.value);"

在 JavaScript 代码中,你可以简单地创建一个这样的函数:

function validateForm(email1, email2) {
    return 0 == email1.localeCompare(email2);
}

也许您还想检查电子邮件字段是否为空,或者您的宠物选择是否正确 - 所有这些检查也可以在该功能中实现。

(另请参阅在 Javascript 中比较字符串的最佳方法?(

2(

一(

要获得逗号分隔的宠物列表,请尝试PHPs内爆方法:

$message .= "Pets: " . implode(", ", $_POST['pet']);

您的问题是直接打印数组,而不是其值。使用内爆,您可以获取所有值,在它们之间添加,并从中形成一个字符串。

如何打印所有值并不重要,但数组是什么和它的值是什么之间的概念很重要。也许你应该尝试print($_POST['pet']);print_r($_POST['pet'])看看有什么不同。并阅读有关这些函数的 PHP 文档。

b(

重定向有点棘手。您可以使用这样的 html 重定向:

<meta http-equiv="refresh" content="0; url=http://example.com/" />

其中0是重新连接到http://example.com/之前的时间(以秒为单位(。

(另请参阅从 HTML 页面重定向(

但您可能想先检查表单并确保在重定向之前发送电子邮件。这应该通过发送新的标头来完成。

header("Location: http://example.com/");

这里的问题是必须在发生任何输出之前发送标头信息。

(另请参阅如何修复 PHP 中的"标头已发送"错误(

要在示例中实现此目的,您只需删除邮件创建周围的 HTML 部分即可。然后,您只需将表单提交到contactProcess.php然后从该页面提交到contact_success.html。

<?php
    //Set up message body
    $message = "Form Submitted on: " . date("m/d/y") . "'n";
    $message .= "'n";
    $message .= "Name: " . $_POST['name'] . "'n";
    $message .= "Email Address: " . $_POST['email'] . "'n'n";
    $message .= "Referred by: " . stripslashes($_POST['referral']) . "'n";
    $message .= "Pets: " . implode(", ", $_POST['pet']);

    //Set up email
    $to = "'"Some person'" <example@example.com>, '"" . $_POST['name'] . "'" <" . $_POST['email'] . ">";
    $subject = "New Email Club Entry";
    $headers = "From: " . "'"All 4 Pets Website'" <all4pets@example.com>";
    //Send email
    mail($to, $subject, $message, $headers);
    // Redirect to contact_success.html
    header("Location: contact_success.html"); 
        // maybe you have to adjust the path
?>

此时还有两个提示:

1.:您应该决定文件名的设计模式,并在单词之间使用下划线或用驼峰大小写区分它们(在您的情况下,您使用带有下划线和联系人contact_success.html在驼峰大小写中.php(。你决定哪个并不重要,但你应该尽量保持一致。这使您和其他人更容易阅读代码、组织文件等。

2.:"发件人:"电子邮件中的信息应采用类似"All 4 Pets Website" <noreply@example.com>或类似格式的格式。如果您像All 4 Pets Website一样离开,您(和您的客户(可能会收到一封"来自"All@host.com, 4@host.com, Pets@host.com, Website@host.com的电子邮件。因此,请尝试使用格式 "Name" <mail@example.com> .我在上面的代码中包含了这一点,您还可以看到如何在那里转义引号。只需根据需要更新地址和名称即可。

3(为什么是这些代码?

JavaScript,因为你已经为它提供了模板。我当然会添加更多的检查,但我更喜欢在PHP脚本中评估 - 因为互联网上存在没有JavaScript的用户。

内爆?因为在我写这篇文章的时候,xd6_已经提供了答案,所以我只是坚持下去。我可能会想出更复杂的东西,比如 for 循环。

header("Location: http://example.com/");只是因为我总是使用它进行重定向。

4(

<?php
if(isset($_POST['submit'])) echo "<p>Form submitted!</p>";
if(isset($_POST['arr'])) {
   echo "<p>Received an array: <br /><br />";
   echo implode(", ", $_POST['arr']);
   echo "<br /><br />";
   print_r($_POST['arr']);
   echo "</p>";
}
?>
<form method="post" action="">
  <input type="checkbox" value="checkbox1" name="arr[]" />
  <input type="checkbox" value="checkbox2" name="arr[]" />
  <input type="checkbox" value="checkbox3" name="arr[]" />
  <input type="checkbox" value="checkbox4" name="arr[]" />
  <input type="submit" value="submit" name="submit" />
</form>

这个简单的示例显示了一个带有几个复选框的窗体。只需将代码复制到 PHP 文件并试用即可。

5(

正如评论中所指出的,我在 JS 中有一个错误:当然你必须使用 email.value ,而不是字段本身进行简单比较。我更改了上面的代码。下面我扩展了 4( 中的示例,以提供一些表单检查。同样,它应该通过将其复制到 PHP 文件中来工作。

<script type="text/javascript">
function validateForm(str1, str2) {
   /* this if statement is just for demonstration purposes */
   if(0 != str1.localeCompare(str2))
       document.getElementById('error').innerHTML = "Text fields are not the same!";
   return 0 == str1.localeCompare(str2);
}
</script>
<?php
if(isset($_POST['submit'])) echo "<p>Form submitted!</p>";
if(isset($_POST['arr'])) {
   echo "<p>Received an array: <br /><br />";
   echo implode(", ", $_POST['arr']);
   echo "<br /><br />";
   print_r($_POST['arr']);
   echo "</p>";
}
?>
<div id="error"></div>
<form method="post" action="" onsubmit="return validateForm(str1.value, str2.value);">
  <input type="checkbox" value="checkbox1" name="arr[]" />
  <input type="checkbox" value="checkbox2" name="arr[]" />
  <input type="checkbox" value="checkbox3" name="arr[]" />
  <input type="checkbox" value="checkbox4" name="arr[]" />
  <input type="text" name="str1" />
  <input type="text" name="str2" />
  <input type="submit" value="submit" name="submit" />
</form>

6(

更新了我的 2(b(2。陈述。 "Name" <mail@example.com>是要走的路。