dongxiaoxiao1983
dongxiaoxiao1983
2012-07-02 02:35

停止空表单提交PHP

  • php
  • validation
  • forms

I have been trying to implement server validation to prevent blank emails in my contact us page, but I am not sure on how to do it in PHP, here is my code:

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_tel = $_POST['cf_tel'];
$field_message = $_POST['cf_message'];


$mail_to = 'test@test.com, test@test.com, test@test.com';
$subject = 'Just iStuff Mobile Contact Us: '.$field_name;

$body_message = 'From: '.$field_name."
";
$body_message .= 'E-mail: '.$field_email."
";
$body_message .= 'Telephone Number: '.$field_tel."
";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."
";
$headers .= 'Reply-To: '.$field_email."
";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for your email, we have received your message and will reply within the next few days.');
        window.location = 'contactus.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed, please try again or email test@test.com');
        window.location = 'contactus.html';
    </script>
<?php
}
?>

Can anyone help me to do this, the tutorials online do not cover this way of doing it...

Thanks

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答