使用PHP检查未填写的字段 [英] Checking for unfilled fields with PHP
问题描述
我正在创建一个结帐页面,要求客户填写他的个人信息以及他的信用卡详细信息(此部分使用条纹).
I am creating a checkout page that requires the client to fill out his personal information as well as his credit card details (this part using stripe).
我想知道,检查字段是否已填写的最佳方法是什么?我该在$ _POSTs字段并处理付款的processingPayment.php中做到这一点吗,如果这些字段没有填写,我会重定向回结帐吗?
I was wondering, what is the best way to check whether the fields are filled up or not? Shall I do it in the processingPayment.php that $_POSTs the fields and processes payment, and in case the fields were not filled, I would redirect back to checkout?
还是在提交表单之前使用js进行现场检查更好的主意?
Or is it a better idea to use js to check on the spot before submitting the form?
如果在处理页面中,我会尝试这样的事情:
if in the processing page, I would try something like this:
if (empty($firsName) || empty($lastName) || empty($address) || empty ($city) || empty ($state) || empty($zip))
{
header('Location: checkout.php');
}
但是我需要重新发送输入的值,以便结帐页面接收到它们,而用户不必再次重新填写每个字段...
But I would need to re-send the values that were entered so the checkout page receives them and the user doesn't have to re-fill every field again...
推荐答案
像这样吗?
foreach($_POST as $key=>$val) {
if( empty($val) ) {
echo "$key is empty";
}
}
PHP的 best 最佳方法是使用一组可能的参数:
The best method with PHP is to have an array of possible arguments:
$array = array('firstName', 'lastName');
foreach($array as $val) {
if( empty($_POST[$val]) ) {
echo "$val is empty";
}
}
否则,客户端验证也可以,但是始终可以禁用.为了完全安全,请同时使用客户端和服务器端.
Otherwise, client side validation works too, but can always be disabled. To be completely safe, use both client and server side.
这篇关于使用PHP检查未填写的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!