简体   繁体   English

如何检查html表单中的必填字段是否已填写?

[英]How do I check if the required fields in an html form are filled?

I've got a submission page in php with an html form that points back to the same page. 我在php中有一个带有html表单的提交页面,该页面指向同一页面。 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. 我想知道如何分别使用php和javascript。 However, I imagine this is a common issue so any other answers are welcome. 但是,我认为这是一个常见问题,因此欢迎您提供其他答案。

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;">
}

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. 由于您需要两者(为了方便用户,所以需要客户端,因为现在我们实际上已经远远超过了信任客户端),因此从单个基础结构中同时支持这两者似乎是一个不错的主意。

As far as JS goes you have to check before you submit. 就JS而言,您必须在提交之前进行检查。 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. 通常,这涉及将一些验证功能绑定到表单的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. 大多数JS库都有验证实现,这些实现将为您完成大部分工作,这似乎对您来说是个好主意。 Googling "client side validation" will yield infinite results, but this (I'm library agnostic, read and choose for yourself) should get you started*: 谷歌搜索“客户端验证”将产生无限的结果,但这(我与图书馆无关,请自行阅读并选择)应该可以帮助您开始*:

http://blog.jquery.com/2007/07/04/about-client-side-form-validation-and-frameworks/ 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://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 http://dojotoolkit.org/book/dojo-book-0-4/part-4-more-widgets/forms/validation

*this is on the teaching you to fish plan *这是在教你钓鱼计划

LiveValidation库将为您提供很多帮助: http : //www.livevalidation.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查必填字段是否已填写? - How to check if required fields are filled in or not? 带有必填字段的html表单 - html form with required fields 制作一个RSVP表格。如何根据下拉列表使必填字段不为必填项? - Making an RSVP form.. How do I make required fields not required based on dropdown? 如何仅在使用 bootstrap 填写所有必需的 attr 字段后才提交表单? - How to submit a form only if all the required attr fields are filled using bootstrap? PHP:如果表单中的必填字段未填写,如何使用户停留在同一页面上? - PHP: How to make the user stay on the same page if the required fields in the form aren't filled? 我需要将必需的表单字段添加到我的联系表单中,但是不确定如何做吗? - I need to add required form fields to my contact form but not sure about how to do it? 如何基于HTML表单字段中填充的信息从sql查询中检索特定信息? - How can I retrieve specific information from my sql query based on what information is filled in the HTML form fields? Yii框架:检查用户是否已填写必填配置文件字段 - Yii framework: check if the user has filled the required profile fields 如何用我的数据库中的字段填写html表单? - how do I fill a html form with fields from my database? Laravel表单在提交之前不显示需要填写的字段 - Laravel form not showing fields required to be filled in before submitting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM