0

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

这是我的 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
}
?>

我该如何解决?

4

2 回答 2

3

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

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

需要添加一个名称字段:

<input type="text" name="Name" value="Name" ...
<input type="text" name="Email" value="Email" ...
于 2012-12-25T17:01:27.267 回答
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> 
于 2012-12-25T17:04:14.787 回答