我有一个联系表格,我需要在 PHP 中进行验证,以检查每个字段是否正确填写。
这是我所拥有的:
//Post fields
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_services = $_POST['services'];
$field_it = $_POST['it'];
$field_location = $_POST['location'];
$field_message = $_POST['message'];
//mail_to omitted
//Validation of contact form
$errormessage = '';
if($field_name == ''){
$errormessage += 'You have not entered your Name\n';
}
if($field_email == ''){
$errormessage += 'You have not entered your Email Address\n';
}
if($field_services == ''){
$errormessage += 'You have not chosen the service you require\n';
}
if($field_it == ''){
$errormessage += 'You have not chosen the Date of your event\n';
}
if($field_location == ''){
$errormessage += 'You have not entered the location of your event\n';
}
if($errormessage != ''){ ?>
<script language="javascript" type="text/javascript">
alert('The following fields have not neen entered correctly\n<?php echo "$errormessage" ?>');
window.location = 'contact.html';
</script>
<?php }
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to s_ejaz@live.co.uk');
window.location = 'contact.html';
</script>
<?php
}
?>
当我尝试提交一个空的联系表单时,这没有任何作用,它应该提醒用户未填写但没有填写的特定字段。它只是把我带到一个空白的白页。
谁能帮我找出我哪里出错了?