codeigniter JSON中的限制复选框[已关闭]

7gs2gvoe  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(139)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

上个月关门了。
Improve this question
有人能帮助我吗,我一直在尝试做一些测验,我把我的问题在JSON文件.然后,我想在checkbox中回答这个小测验,用户必须回答两个checkbox选项。我的问题是如何限制checkbox,使用户只能回答两个checkbox,他们不能选择超过两个或只有一个checkbox?我可以在JSON中使用if else吗?或者我应该把问题放在views而不是JSON中吗?或者我应该添加content_url来链接到views文件,并在views文件中放置一些设置,如限制等?或者有人对我的情况有什么建议吗?我正在使用带有CI 3的php,顺便说一句,我还在学习,很抱歉我的英语不好,因为我不会说英语。
这是我在json中编写的代码:

"part": "question",
            "type": "choice_checkbox",
            "choices": [
                {
                        "choice_text": "A. Pasif",
                        "choice_variable": "A",
                        "choice_value": 1
                },
                {
                        "choice_text": "B. Delusi ",
                        "choice_variable": "B",
                        "choice_value": 1
                },
                {
                        "choice_text": "C. Konsumtif",
                        "choice_variable": "C",
                        "choice_value": 1
                },
                {
                        "choice_text": "D. Patuh",
                        "choice_variable": "D",
                        "choice_value": 1
                },
                {
                        "choice_text": "E. Aktif",
                        "choice_variable": "E",
                        "choice_value": 1
                },
                {
                        "choice_text": "F. Teratur",
                        "choice_variable": "F",
                        "choice_value": 1
                }
            ]
cx6n0qe3

cx6n0qe31#

如果您的问题有多个答案(选择),我建议您进行两种不同的验证:
第一个在客户端,使用Javascript。
CodeIgniter中的第二个示例,创建一个自定义验证方法。如果没有应用程序的代码,创建一个示例并不容易,但这可能会有所帮助:

HTML(您应提交的表单的一部分)

<p>Question 1:</p>
<input type="checkbox" name="questions[0][]" id="choice_1_1" value="Choice 1 Value">
<label for="choice_1_1">Choice 1 to Question 1</label>
<input type="checkbox" name="questions[0][]" id="choice_1_2" value="Choice 2 Value">
<label for="choice_1_2">Choice 2 to Question 1</label>
<input type="checkbox" name="questions[0][]" id="choice_1_3" value="Choice 3 Value">
<label for="choice_1_3">Choice 3 to Question 1</label>

<p>Question 2:</p>
<input type="checkbox" name="questions[1][]" id="choice_2_1" value="Choice 1 Value">
<label for="choice_2_1">Choice 1 to Question 2</label>
<input type="checkbox" name="questions[1][]" id="choice_2_2" value="Choice 2 Value">
<label for="choice_2_2">Choice 2 to Question 2</label>
<input type="checkbox" name="questions[1][]" id="choice_2_3" value="Choice 3 Value">
<label for="choice_2_3">Choice 3 to Question 2</label>

<!-- etc... -->

代码引发器

<?php

class Example extends CI_Controller {

        public function index()
        {
                // Loading the CI form helper (optional)
                $this->load->helper('form');
                
                // The CI form validation library
                $this->load->library('form_validation');

                // Set other form validations here...

                // Set custom validations here...
                $this->form_validation->set_rules('questions[]', 'Questions', 'callback_questions_check');

                // Run the validations...
                if ($this->form_validation->run() == FALSE)
                {
                        // If the validations failed (or the form wasn't submitted)...
                }
                else
                {
                        // If everything is OK...
                }
        }

        public function questions_check($questions)
        {
                // Read questions[] field (works as an array)...
                foreach ($questions as $question)
                {
                        // Check if every question has two checkboxes checked (works as an array too)...
                        if (count($question) != 2)
                        {
                                // If not, set an error message and return FALSE...
                                $this->form_validation->set_message('questions_check', 'The {field} must have two values checked');
                                return FALSE;
                        }
                }

                // If there are no errors, return TRUE...
                return TRUE;
        }
}

正如我所说的,检查这段代码是否适合您的代码并不容易,但是如果您有任何疑问,可以在这里找到更多信息:http://codeigniter.com/userguide3/libraries/form_validation.html?highlight=validation#callbacks-your-own-validation-methods

相关问题