停止空表单提交PHP [英] Stopping empty form submission PHP

查看:56
本文介绍了停止空表单提交PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试执行服务器验证以防止在与我们联系"页面中出现空白电子邮件,但是我不确定如何在PHP中执行此操作,这是我的代码:

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."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Telephone Number: '.$field_tel."\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 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...

谢谢

推荐答案

在$ mail_to之前..

Before your $mail_to..

您可以先在服务器端验证_POST/_GET.

You can validate the _POST/_GET first on server side.

<?php
if (empty($field_name) && empty($field_email) && empty($field_tel) && empty($field_message)) {
    echo 'Please correct the fields';
    return false;
}
?>

或者,您可以先在客户端进行验证.这样可以节省您的时间和资源.

Alternatively, you can validate first on the client-side. It will save you time and resources.

这篇关于停止空表单提交PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆