HTML5,CSS3,PHP联系人表单不发送信息+添加必填字段

HTML5, CSS3, PHP contact form not sending info + add required fields

本文关键字:信息 添加 字段 PHP CSS3 联系人 表单 HTML5      更新时间:2023-09-26

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

这是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>

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

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

HTML中没有名称属性。确保存在名称属性,以便在PHP中可以访问这些属性。

此外,请注意不要将属性命名为"名称…",删除点

您可以使用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);
}

此外,您还必须为<input />字段提供正确的名称。你可以这样做:

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

您犯了一个简单的错误-当html输入元素发布到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...';}">

如果您的$_POST应该为您的归因返回一些东西,那么您需要在创建的每个输入字段中指定属性name,并在$_POST中声明相应的值。

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

不难发现哪里出了问题。

在您的代码片段的第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有一个新的属性,您可以在每个名为requiredinput标记上包含该属性
只需在input标签的末尾添加这样的属性:

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

你可以在这里找到HTML5的许多新属性:http://www.w3schools.com/html/html_form_attributes.asp