单击发送时,我的联系表单会将空白值邮寄给我

时间:2012-12-25 16:59:16

标签: php html contact contact-form

当提交此表单时,我会收到一封电子邮件,其中包含所有值为空白的请帮助我。

这是我的html文件,其中我接受姓名,电子邮件和消息作为输入。

<form id="form" method="post" action="contact.php" >
    <fieldset>
        <label><input type="text" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''"></label>
        <label><input type="text" value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''"></label>
        <label><textarea onBlur="if(this.value==''){this.value='Message'}" onFocus="if(this.value=='Message'){this.value=''}">Message</textarea></label><br>
        <<a href="#" class="button" onClick="document.getElementById('form').submit()">Send</a></div>
    </fieldset>  
</form> 

php文件:

在php文件中我想通过电子邮件发送有关id jay44556677@gmail.com的所有详细信息。

<?php
$field_name = $_POST['Name'];
$field_email = $_POST['Email'];
$field_message = $_POST['Message'];

$mail_to = 'jay44556677@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\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 = 'contacts.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please try again.');
        window.location = 'contacts.html';
    </script>
<?php
}
?>

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

您永远不会为表单元素指定name=,因此值永远不会提交给您的服务器。

<input type="text" value="Name" ...
<input type="text" value="Email" ...

需要添加名称字段:

<input type="text" name="Name" value="Name" ...
<input type="text" name="Email" value="Email" ...

答案 1 :(得分:0)

每个输入表单元素都应该有名称字段,否则会发送空白值。

<form id="form" method="post" action="contact.php" >
                  <fieldset>
                    <label><input type="text" name="Name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''"></label>
                    <label><input type="text" name="Email" value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''"></label>
                    <label><textarea name="Message" onBlur="if(this.value==''){this.value='Message'}" onFocus="if(this.value=='Message'){this.value=''}">Message</textarea></label><br>
                    <<a href="#" class="button" onClick="document.getElementById('form').submit()">Send</a></div>
                  </fieldset>  
                </form>