我在 PHP 中得到以下代码:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'info@remydesigns.nl';
$subject = 'Bericht van '.$field_name;
$body_message = 'Van: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Bericht: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
else { ?>
<script language="javascript" type="text/javascript">
alert('Bericht mislukt, probeer het nog een keer.');
window.location = 'index.htm';
</script>
<?php
}
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Bedankt voor je bericht.');
window.location = 'index.htm';
</script>
<?php
}
?>
每当我点击提交时,即使没有填写字段,它也会发送一封电子邮件。所以我不断收到人们的空邮件..
我不能让它检查字段是否为空。我的代码有什么问题?
谢谢
编辑:
<?php
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])) {
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'info@remydesigns.nl';
$subject = 'Bericht van '.$field_name;
$body_message = 'Van: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Bericht: '.$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('Bedankt voor je bericht.');
window.location = 'index.htm';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Bericht mislukt, probeer het nog een keer.');
window.location = 'index.htm';
</script>
<?php
}
?>
HTML:
<form action="contact.php" method="post">
<span>Naam</span> <input class="name" name="name" type="text" />
<span>Email</span> <input class="email" name="email" type="text" />
<span>Bericht</span><textarea class="message" name="message"></textarea>
<input class="submit" type="submit" value="Submit" />
</form>