- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 PHP Codeigniter 制作一个基本的注册页面。
在注册页面上,我要求用户从一系列类别中进行选择(通过 <select>
html 元素)。这些类别存储在我的 MySQL 数据库的列表中。
我当前的方法是当用户调用函数加载页面时从数据库中获取此列表,然后将其显示给他们。但是,如果用户输入的数据不正确并且页面必须重新加载并出现验证错误,则保存列表中数据的变量似乎被清除,并且我必须在重新显示页面之前从数据库重新获取列表数据。
我相信文档中有一些关于如何将此变量设置为永久可用的内容,但再次查看时,我没有找到它。
有人能指出我正确的方向吗?对我来说,每次都需要重新获取这些数据似乎很愚蠢(我知道人们不会经常输入错误的信息,但这在某些情况下会派上用场)。
注意:这不是记住用户选择的问题。
最佳答案
此示例适用于使用 ci 表单助手的选择下拉列表(我正在从另一种形式修改它,希望它全部正确)
the Array of select values is: $categoryarray
the drop down field name is 'category'
the default value is $defaultcategory
a css class to style (bootstrap etc): $dropclass = 'class="input-medium"';
代码行是:
form_dropdown('category',$categoryarray,set_value('category',$defaultcategory),$dropclass).form_error('category');
form_error('类别');最后用于显示验证错误消息即使有默认值 - 如果表单未通过表单中另一个字段的验证 - 这将“记住”用户选择的内容。
编辑!
好的,有好消息也有坏消息。坏消息 - 如果类别来自数据库,那么您需要再次获取它们。好消息 - CI 会记住用户从下拉列表中选择的类别。
坏消息实际上并不是什么大问题 - 如果您在模型中创建类别数组。那么只需添加一行代码即可进行验证。
// In the Model
function makedropdown() {
// get your category list
$cats = $this->getAllCategories() ;
$categoryarray = array();
// make the array
foreach ( $cats->result() as $row ) {
$categoryarray[$row->category] = $row->category ; }
return $categoryarray ;
}
有人填写了表单,我们运行验证,验证失败。在 Controller 中:
if ( $this->form_validation->run($formrules) == FALSE ) {
// get the categoryarray
$data['categoryarray'] = $this->categorymodel->makedropdown() ;
// load form again
$this->load->view( 'myform', $data ); }
因此,即使我们再次从数据库获取类别以动态填充选择列表 - CI 仍然会记住用户第一次填写表单时的选择。
下拉列表的默认类别怎么样?如果它不会改变,那么它可以设置为配置。如果类别值来自数据库并且可以更改 - 那么可以在模型中创建默认类别。
编辑2
天哪,无论如何我总是这样做,所以为什么我没有想到这一点。所以是的,这是制定特定方法来显示您的 View 的另一个原因
function _showCategoryForm(){
// get the categoryarray
$data['categoryarray'] = $this->categorymodel->makedropdown() ;
// anything else thats needed for the view
// load form view
$this->load->view( 'myform', $data ); }
现在我们没有任何重复的代码,并且如果需要,可以轻松地使用错误消息自定义验证失败。
// since i'm grinding on this - the validation should happen in a model
// and that method returns true or false
if ( $this->somemodel->validateCategoryForm() == FALSE ) {
// custom obnoxious error message
$this->formerrormessage = "What part of required is eluding you?" ;
$this->_showCategoryForm() ; }
这要好得多,因为如果表单的需求发生变化 - 更改仅在一个地方。此外,我添加了一个下划线来提醒我们所有人,私有(private)方法是一种很好的做法。并且表单验证应该在模型中分开,由 Controller 调用。
关于PHP 代码点火器 : Passing dynamic form data to a view without having to refetch from the database upon validation errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18970808/
如何将多个json对象添加/映射到dart对象 import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundati
我正在研究将产品集成到 Dynamics Great Plains 中。我之前使用过 Dynamics CRM,但没有使用过 Great Plains。我听说 GP 与 CRM 是不同的动物,即使它们
System.Linq.Dynamic.Core 和 System.Linq.Dynamic 有什么区别?我目前正在使用 System.Linq.Dynamic 并且它不包含对 的支持选择 和 多选
我正在尝试通过Firebase存储将图像上传到Firebase,然后在Firestore中创建一个文档,其中包含上述上传图像的网址。为此,我使用此功能 void uploadImageAndCr
我一直在尝试整理一些东西,使我可以从 ListPlot 中提取点,以便在进一步的计算中使用它们。我目前的方法是使用 Locator[] 选择点。这适用于显示点,但我无法弄清楚如何从带有 head Dy
只要我在与 Program 类相同的程序集中有类 ClassSameAssembly ,下面的代码就可以正常工作。但是,当我将类 ClassSameAssembly 移动到单独的程序集时,会引发 Ru
我只是尝试从Firebase实时数据库解析数据。 但在转换为模型时有问题 我正在尝试从Flutter上解析Firebase数据库中的数据。 但是一个错误说 MY Complete QUIZ: {-M5
我创建了一个方法,当我构建它时,出现了这个错误: type '_InternalLinkedHashMap' is not a subtype of type 'List' in type cast
我对这个 flutter 的简单图表代码有疑问。在我尝试运行代码时显示此错误。请任何人都可以帮助我在这.... The argument type 'List>' can't be assigned
我尝试在我的 flutter 应用程序中解析来自 Firestore 的文档。 Firestore 文档: 我创建了两个类来解析这个文档。 类产品: class Produkt{ String n
我有一个2d-List,其中包含一个字符串和一个Map,如下所示: List> content = [ [ "String", { "one": 23,
我使用 Dart“json_serializable”包在 Flutter 应用程序中的 Firestore 数据结构下反序列化。 { googleBookId: jjl4BgAAQBAJ, prov
我注册了 Dynamic CRM 在线试用版(30 天)并创建了一个非托管自定义解决方案(新字段、一些自定义实体等)。现在,我想导出非托管解决方案并将其导入到我的服务器中的 Dynamic CRM O
当我尝试从 StreamTransform 获取一些数据时遇到一些问题 我不明白什么是正确的数据类型 未捕获的异常:类型错误:“_StreamHandlerTransformer”的实例:“_Stre
我正在尝试获取用户数据,但在这样做时出现以下错误: Exception: type '_InternalLinkedHashMap' is not a subtype of type 'Map 我查看
我正在尝试在 sqflite 数据库中保存一些带有 flutter 的数据,但我仍然收到一条错误消息: [ERROR:flutter/shell/common/shell.cc(181)] Dart
我有一个JSON响应,结构如下:。在这个JSON响应中,有各种动态键,如“Owner”和“Master”,每个键都包含一个JSON对象或一个JSON对象数组。我需要创建一个gson数据类来解析这个动态
在 Dynamics 2012 ax 中编译 CIL 时,我看到以下错误 - 名称为“Dynamics.Ax.application”的重复类型。在程序集中“Dynamics.Ax.applicati
我有一个带有 LinkedHashMap 成员的 StatefulWidget 小部件,如下所示: LinkedHashMap _items = new LinkedHashMap>(); 现在我需要
我正在尝试按照此处所述实现搜索资源功能:https://cloudblogs.microsoft.com/dynamics365/it/2019/05/21/retrieve-resource-ava
我是一名优秀的程序员,十分优秀!