Codeigniter set_select
抱歉,我现在无法发布任何代码,因为我离开了办公桌,但我会尝试尽可能清楚地解释我的问题,希望有人可以提供帮助!
我有一个长表单,提交给控制器中的方法。为了使事情更加用户友好,我将表单分为几个部分。每个部分都有一个隐藏字段,充当标志。表单提交的方法由测试这些标志的条件组成。如果找到标志,则可以显示表单的下一部分。我还可以分段收集数据,而不是一次性处理全部数据。
当我尝试使用 set_select 方法为选择菜单设置默认值时,出现了问题。该方法是 CI 系统 form_validation 库的一部分。如果可选的第三个参数作为 true 传递给该方法,则应将该值设置为默认值。但是,这仅在先前未运行 form_validation 的情况下才有效。这是完全有道理的,因为假设只有在第一次看到表单时才需要默认值。在任何提交之前,因此在任何发布数据之前。
当然,我的情况并非如此。如果我只是清空 post 数组,那么这就违背了 set_select 函数的要点,因为表单上方的任何选择菜单将不再能够设置为其最后一个值。
有人能想出一个好的解决方案吗?我知道使用 AJAX 会容易得多,但我想确保每个人都能获得最佳体验,无论他们是否决定使用 JavaScript。
Apologies I can't post any code at this time cos I'm away from my desk, but I will try and explain my problem as clearly as possible in the hope someone can help!
I have a long form that submits to a method in a controller. To make things more user friendly I have split the form into sections. Each section has a hidden field that acts as a flag. The method that the form submits to is made up of conditionals that test for these flags. If a flag is found then the next part of the form can be displayed. I can also gather the data in sections rather than processing the whole lot in one go.
My problem arises when I try to use the set_select method to set a default value for select menus. The method is part of the CI system form_validation library. If an optional third parameter is passed to the method as true it should set that value as the default. However, this only works if the form_validation hasn't previously been run. This makes perfect sense because it is assumed that a default would only be needed the first time a form is seen ie. before any submission and therefore before any post data.
Ofcourse this isn't how it is in my situation. If i simply empty the post array then that defeats the point of the set_select function because any select menus higher up the form would no longer be able to be set to their last value.
Can anyone think of a good solution? I know this would be much easier with AJAX, but I want to make sure that everyone gets the best possible experience whether or not they decide to use JavaScript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我个人而言,我会使用 AJAX 将表单分成 3 个部分,每次都通过 AJAX 提交。
那么对于非 javascript 用户(少数),我只会完整地显示表单,并正常处理。
打破形式听起来像是一个“特性”而不是一个功能。这意味着JavaScript用户将能够利用分解表单的“功能”,而非JavaScript用户仍然能够轻松完成提交信息的“功能”。
Personally, I would use AJAX to break the form up into your 3 sections, submitting by AJAX each time.
THEN for non-javascript users (the few there are), I would just display the form in its entirety, and process normally.
breaking the form up sounds like a "feature" not a function. Meaning that javascript users will be able to take advantage of the "feature" of breaking the form up, while non-javascript users will still be able to effortlessly accomplish the "function" of submitting the information.
这样做怎么样:
好的
然后您可以继续进行任何您想要的过程,what about doing it like this:
OK
then you can proceed to whatever process you want我认为使用 set_select 对我来说可能不是最好的选择,因此我在视图文件中添加了一些代码,这些代码将为每个单独的情况执行此操作,而不是尝试使用通用函数。
不理想,但正在工作!
I've decided that using set_select is probably not the best option for me, so I've added some code into my view files which will do this for each individual case rather than trying to use a generic function.
Not ideal, but is working!