简体   繁体   English

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

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

I've added a contact form to my website. 我已经在我的网站上添加了联系表格。 The form sends out the email, however i'm not receiving the information. 表格发送了电子邮件,但是我没有收到信息。 All the fields come out blank. 所有字段都为空白。 Also, i'd like to make the fields required. 另外,我想填写必填字段。 Anyone? 任何人?

Here's the PHP 这是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 }
?>

Here's the Form 这是表格

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

Its blank because your inputs do not have a name attribute. 它为空,因为您的输入没有名称属性。 All inputs need the name attribute. 所有输入都需要name属性。 Example: 例:

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

There are no name attributes in your HTML. HTML中没有名称属性。 Ensure that there are name attributes so that they are accessible in PHP. 确保存在名称属性,以便可以在PHP中访问它们。

Also, please be careful as to not name the attributes as "Name...", Remove the dots 另外,请注意不要将属性命名为“名称...”,请删除点

If your $_POST should return something for your attribution, you need to specify attribute name inside each input field you created with those respective value declared inside $_POST's. 如果您的$_POST应该归因于您的属性,则需要在创建的每个输入字段中指定属性name并在$ _POST的内部声明这些值。

Besides, you will probably face blank pages, which means you have a syntax error. 此外,您可能会面临空白页,这意味着您遇到语法错误。

It was not difficult to find what is wrong. 找出问题所在并不难。

At the line 4 of your snippet code, I can see there is a letter v among with your declared variable: 在代码段的第4行,我可以看到在声明的变量中有一个字母v

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

Remove it and try to refresh the page again. 删除它,然后尝试再次刷新页面。
Also, It is important you to look at php_error_log and see what kind of error your are facing it. 另外,重要的是要查看php_error_log,看看您面对的是哪种错误。

EDIT 编辑

If you want to turn required field, HTML5 has a new attribute that you can include on each input tag called required . 如果您想打开必填字段,HTML5具有一个新属性,您可以将其包含在名为required每个input标签上。
Just add at the end of your input tag this attribute like this: 只需在input标签的末尾添加此属性,如下所示:

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

There are a lot of new attributes for HTML5 you can find in here: http://www.w3schools.com/html/html_form_attributes.asp 您可以在此处找到HTML5的许多新属性: http : //www.w3schools.com/html/html_form_attributes.asp

You can use an if to check if the values are empty or not, eg: 您可以使用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);
}

Also you have to give your <input /> fields the correct name. 另外,您还必须为<input />字段<input />正确的名称。 You could do this: 您可以这样做:

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

You're making a simple mistake - when an html input element posts to a php form, the index that it fills in the $_POST array is based on the name attribute of the html element, but you are using the value attribute instead. 您正在犯一个简单的错误-当html输入元素发布到php表单时,它填充$_POST数组的索引基于html元素的name属性,但是您使用的是value属性。

I would also avoid strange characters like periods in your name attribute. 我还要避免在您的name属性中使用诸如句点之类的奇怪字符。

Try replacing 尝试更换

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM