【问题标题】:HTML5, CSS3, PHP contact form not sending info + add required fieldsHTML5、CSS3、PHP 联系表单不发送信息 + 添加必填字段
【发布时间】:2015-04-05 18:51:28
【问题描述】:

我在我的网站上添加了一个联系表。表格发送电子邮件,但我没有收到信息。所有字段都是空白的。另外,我想填写必填字段。任何人?

这里是 PHP

<?php
$field_name = $_POST['Name...'];
$field_email = $_POST['Email...'];
$field_phone = $_POST['Phone...'];
$field_company = $_POST['Company'];
$field_message = $_POST['Message...'];

$mail_to = 'email@me.com';
$subject = '#Message from Website# '.$field_name;

$body_message .= 'From: '.$field_name."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Phone: '.$field_phone."\n";
$body_message .= 'Company: '.$field_company."\n";
$body_message .= 'Message: '.$field_message."\n";

$headers = 'From: '.$E-Mail."\r\n";
$headers .= 'Reply-To: '.$E-Mail."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
 <script language="javascript" type="text/javascript">
  alert('Thank you for the message. We will contact you shortly.');
  window.location = 'index.html';
 </script>
<?php
}
else { ?>
 <script language="javascript" type="text/javascript">
  alert('Message sending failed. Please, send an email to         
email@me.com');
  window.location = 'index.html';
 </script>
<?php }
?>

这是表格

<form action="contact.php" method="post">
    <input type="text" class="text" value="Name..." onfocus="this.value = '';" onblur="if (this.value =='') {this.value = 'Name...';}">
    <input type="text" class="text" value="Email..." onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email...';}">
    <input type="text" class="text" value="Phone..." onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Phone...';}">
    <input type="text" class="text" value="Company..." onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Company...';}">
    <textarea value="Message..." onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Message';}">Message...</textarea>
    <input class="wow shake" data-wow-delay="0.3s" type="submit" value="Send Message" />
</form>

【问题讨论】:

  • 这一行多了一个v:v$field_phone = $_POST['Phone...'];
  • 这是一个复制和粘贴错误……我的错。
  • 现在,我怎样才能使这些字段成为必填项?

标签: javascript php html css forms


【解决方案1】:

它是空白的,因为您的输入没有名称属性。所有输入都需要 name 属性。示例:

<input type="text" value="" id="first_name" name="first_name" />

【讨论】:

  • 现在,如何填写必填字段?
【解决方案2】:

您的 HTML 中没有名称属性。确保有 name 属性,以便它们可以在 PHP 中访问。

另外,请注意不要将属性命名为“名称...”,删除点

【讨论】:

  • 我先发布了我的答案?为什么我没有投赞成票?
【解决方案3】:

您可以使用if 来检查值是否为空,例如:

if(!empty($field_name)) {
   // the field name is not empty and you can send the mail
   $mail_status = mail($mail_to, $subject, $body_message, $headers);
}

您还必须为您的&lt;input /&gt; 字段提供正确的名称。你可以这样做:

<?php $field_name = $_POST['fieldname']; ?>
<input name="fieldname" />

【讨论】:

  • 生成所需字段的 if 函数不起作用...还有其他建议吗?
【解决方案4】:

您犯了一个简单的错误 - 当 html input 元素发布到 php 表单时,它在 $_POST 数组中填写的索引是基于 html 元素的 name 属性,但您正在使用value 属性。

我也会避免使用 name 属性中的奇怪字符,例如句点。

尝试替换

<input type="text" class="text" value="Name..." onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name...';}">

<input type="text" class="text" name="Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name...';}">

【讨论】:

    【解决方案5】:

    如果您的$_POST 应该为您的归属返回某些内容,您需要在您创建的每个输入字段中指定属性name,并使用在$_POST 中声明的相应值。

    此外,您可能会遇到空白页,这意味着您有语法错误。

    不难发现问题所在。

    在您的 sn-p 代码的第 4 行,我可以看到您声明的变量中有一个字母 v

    <?php
    $field_name = $_POST['Name...'];
    $field_email = $_POST['Email...'];
    v$field_phone = $_POST['Phone...'];
    $field_company = $_POST['Company'];
    $field_message = $_POST['Message...'];
    

    删除它并再次尝试刷新页面。
    此外,查看 php_error_log 并查看您所面临的错误类型也很重要。

    编辑

    如果您想转为必填字段,HTML5 有一个新属性,您可以在每个 input 标记上包含该属性,称为 required
    只需在 input 标记的末尾添加此属性,如下所示:

    &lt;input type="text" class="text" name="Name" value="Name..." onfocus="this.value = '';" onblur="if (this.value =='') {this.value = 'Name...';}" required&gt;

    您可以在此处找到许多 HTML5 的新属性:http://www.w3schools.com/html/html_form_attributes.asp

    【讨论】:

    • @RafaelGranato:由于我的回答已完成,并提取了您主题中的另一个错误,我希望您接受我的回答。我修复了您代码中的两个问题。顺便说一句,我编辑了我的答案,包括如何在您的字段中输入必填字段。 =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多