我一直在四处寻找,并没有找到一种简单的方法来编辑此代码,以便当用户没有在必填字段中输入任何内容(所有字段都是必需的)时,会显示一个警报,要求用户请输入一些东西。
任何人都可以帮忙吗?
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
$mail_to = 'you@yourdomain.com';
$subject = 'Site Mail';
$body_message = 'From: '.$field_name."\n";
$body_message .= 'Email: '.$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('Thanks! Your email has been sent.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Sorry, something went wrong.');
window.location = 'index.html';
</script>
<?php
}
?>