I've got a submission page in php with an html form that points back to the same page. I'd like to be able to check if the required fields in the form aren't filled so I can inform the user. I'd like to know how to do that with php and javascript each. However, I imagine this is a common issue so any other answers are welcome.
我在php中有一个提交页面,其中包含指向同一页面的html表单。我希望能够检查表单中的必填字段是否未填写,以便我可以通知用户。我想知道如何用php和javascript各做一遍。但是,我认为这是一个常见问题,所以欢迎任何其他答案。
4 个解决方案
#1
Do the check in posting part of your php
办理登记部分PHP的签到
if(isset($_POST['save']))
{
$fields=array();
$fields['Nimi'] = $_POST['name'];
$fields['Kool'] = $_POST['school'];
$fields['Aadress'] = $_POST['address'];
$fields['Telefon'] = $_POST['phone'];
$fields['Email'] = $_POST['email'];
foreach ($fields as $key => $val)
{ if(trim($val)=='')
{ $errmsg=$key." is not filled!";
break;
}
}
}
if($errmsg == '')
{ //do your saving here
exit();
}
if(!isset($_POST['save']) || $errmsg != '')
{ //show your form here
// and make it to return to the same page on submit
//<input name="save" type="submit" value="Save" onclick="return true;">
}
#2
For extra credit, once you know how to do it in PHP and JavaScript from Riho and annakata's answers, then build a way of defining a field constraint in a single form that can both be rendered as JavaScript for client-side validation and run on the server.
为了获得额外的功劳,一旦你知道如何在Riho和annakata的答案中用PHP和JavaScript做到这一点,那么构建一种在单一形式中定义字段约束的方法,既可以呈现为用于客户端验证的JavaScript,也可以在服务器。
Since you need both (client-side for user convenience, server-side because we're really very much past trusting the client at this point), it seems like quite a decent idea to support both from a single infrastructure.
既然你需要两者(客户端为了方便用户,服务器端,因为我们现在真的非常信任客户端),从单一基础架构支持这两者似乎是一个不错的主意。
#3
As far as JS goes you have to check before you submit. Generally this involves binding some validation function to the onsubmit event trigger of the form, and that validation function will consist of some tests for each field you're interested.
就JS而言,你必须在提交前进行检查。通常,这涉及将一些验证函数绑定到表单的onsubmit事件触发器,并且验证函数将包含您感兴趣的每个字段的一些测试。
Most JS libraries have validation implementations that will do most of the work for you, which sounds like it might be a good idea for you. Googling "client side validation" will yield infinite results, but this (I'm library agnostic, read and choose for yourself) should get you started*:
大多数JS库都有验证实现,可以为您完成大部分工作,听起来这对您来说可能是一个好主意。谷歌搜索“客户端验证”将产生无限的结果,但这(我的库不可知,阅读和自己选择)应该让你开始*:
http://blog.jquery.com/2007/07/04/about-client-side-form-validation-and-frameworks/
http://tetlaw.id.au/view/blog/really-easy-field-validation-with-prototype/
http://dojotoolkit.org/book/dojo-book-0-4/part-4-more-widgets/forms/validation
*this is on the teaching you to fish plan
*这是教你钓鱼的计划
#4
The LiveValidation library would help you out a lot: http://www.livevalidation.com/
LiveValidation库可以帮到你很多:http://www.livevalidation.com/
#1
Do the check in posting part of your php
办理登记部分PHP的签到
if(isset($_POST['save']))
{
$fields=array();
$fields['Nimi'] = $_POST['name'];
$fields['Kool'] = $_POST['school'];
$fields['Aadress'] = $_POST['address'];
$fields['Telefon'] = $_POST['phone'];
$fields['Email'] = $_POST['email'];
foreach ($fields as $key => $val)
{ if(trim($val)=='')
{ $errmsg=$key." is not filled!";
break;
}
}
}
if($errmsg == '')
{ //do your saving here
exit();
}
if(!isset($_POST['save']) || $errmsg != '')
{ //show your form here
// and make it to return to the same page on submit
//<input name="save" type="submit" value="Save" onclick="return true;">
}
#2
For extra credit, once you know how to do it in PHP and JavaScript from Riho and annakata's answers, then build a way of defining a field constraint in a single form that can both be rendered as JavaScript for client-side validation and run on the server.
为了获得额外的功劳,一旦你知道如何在Riho和annakata的答案中用PHP和JavaScript做到这一点,那么构建一种在单一形式中定义字段约束的方法,既可以呈现为用于客户端验证的JavaScript,也可以在服务器。
Since you need both (client-side for user convenience, server-side because we're really very much past trusting the client at this point), it seems like quite a decent idea to support both from a single infrastructure.
既然你需要两者(客户端为了方便用户,服务器端,因为我们现在真的非常信任客户端),从单一基础架构支持这两者似乎是一个不错的主意。
#3
As far as JS goes you have to check before you submit. Generally this involves binding some validation function to the onsubmit event trigger of the form, and that validation function will consist of some tests for each field you're interested.
就JS而言,你必须在提交前进行检查。通常,这涉及将一些验证函数绑定到表单的onsubmit事件触发器,并且验证函数将包含您感兴趣的每个字段的一些测试。
Most JS libraries have validation implementations that will do most of the work for you, which sounds like it might be a good idea for you. Googling "client side validation" will yield infinite results, but this (I'm library agnostic, read and choose for yourself) should get you started*:
大多数JS库都有验证实现,可以为您完成大部分工作,听起来这对您来说可能是一个好主意。谷歌搜索“客户端验证”将产生无限的结果,但这(我的库不可知,阅读和自己选择)应该让你开始*:
http://blog.jquery.com/2007/07/04/about-client-side-form-validation-and-frameworks/
http://tetlaw.id.au/view/blog/really-easy-field-validation-with-prototype/
http://dojotoolkit.org/book/dojo-book-0-4/part-4-more-widgets/forms/validation
*this is on the teaching you to fish plan
*这是教你钓鱼的计划
#4
The LiveValidation library would help you out a lot: http://www.livevalidation.com/
LiveValidation库可以帮到你很多:http://www.livevalidation.com/