简体   繁体   English

这个联系表格代码有什么问题?

[英]What's wrong with this contact form code?

This is my code for a simple contact form. 这是我的简单联系表格的代码。 All the emails successfully get sent however when they don't enter any information the supposedly error message that should appear doesn't and it sends an email blank. 所有电子邮件都成功发送,但是当他们没有输入任何信息时,应该出现的错误消息不会发送,并且它会发送电子邮件空白。 As I do not wish to have spam emails, what am I doing wrong? 由于我不希望收到垃圾邮件,我做错了什么?

Code: 码:

<?php

$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];

$mail_to = 'myemail@goeshere';
$subject = 'Message from client: '.$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 contacting us');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Your message failed to send due to invalid credentials.');
        window.location = 'index.html';
    </script>
<?php
}
?>

The HTML form: HTML表单:

<form method="post" action="contact.php">
<div class="row half">
<div class="6u"><input type="text" class="text" name="name" placeholder="Name" /></div>
<div class="6u"><input type="text" class="text" name="email" placeholder="Email" /></div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" placeholder="Message"></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<a href="#" class="button submit">Send Message</a>
</div>
</div>
</form>

Sorry if the code isn't indented properly... 对不起,如果代码没有正确缩进...

You don't have any code that checks if the fields are filled in. You should check that the fields are set using isset() and that they contain valid data by using things like filter_var 您没有任何代码来检查是否填写了字段。您应该检查字段是使用isset()设置的,并且它们包含有效数据,方法是使用filter_var东西

Also, you should not be relying on JavaScript to do redirects, If the user has JavaScript disabled this won't work. 此外,您不应该依赖JavaScript来执行重定向,如果用户禁用了JavaScript,则无法使用。 Use PHP to do your redirect. 使用PHP进行重定向。

The code always sends the email, regardless of the validation status of the form. 无论表单的验证状态如何,代码始终会发送电子邮件。 You can, instead, check the form validation before attempting to send the email. 相反,您可以在尝试发送电子邮件之前检查表单验证。 The overall logic would look like this: 整体逻辑看起来像这样:

  • Parse user input 解析用户输入
  • Are required fields valid? 必填字段是否有效?
    • Yes: Send email, display success message 是:发送电子邮件,显示成功消息
    • No: Display error message 否:显示错误消息

In the code, it might be something as simple as: 在代码中,它可能是一样简单:

$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];

if ($field_email != '') {
    // compose and send the email
    // display success message
} else {
    // display error message
}

For additional checks on the other fields, you'd add additional conditions to the if statement. 对于其他字段的其他检查,您需要为if语句添加其他条件。

The mail function will return TRUE if the mail is accepted for delivery. 如果邮件被接受传递, 邮件功能将返回TRUE。 Since all required parameters are present it will accept the mail even though the parameters are empty strings. 由于所有必需参数都存在,即使参数为空字符串,它也会接受邮件。

You would need to check if the input is valid or not. 您需要检查输入是否有效。

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

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