从Web表单上的50多个复选框获取输入 [英] Getting input from 50+ check boxes on a web form

查看:108
本文介绍了从Web表单上的50多个复选框获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想想我正在从事的一种调查形式。它有50多种可能的条件可以全部检查 - 或者一个。就像任何形式一样,我需要能够将结果发布到MySQL数据库。我试图找出最好的方法来获得所有这些复选框的输入,而不必为每个复选框创建单独的列(除非这被认为是最佳实践)。



这是我在想什么可以工作。从1-N数字分配复选框名称。然后遍历每个复选框,检查它的状态。然后,不要为每个复选框创建一列 - 使用一列,并为每个复选框连接某种标识符。



伪码

 对于N中的复选框:
检查N的状态:
如果选中N:
连接字符串+ =标识符(N)

这种方法是一种很好的方法,或者您有其他建议吗?$ b $你的问题不是关于你的表格,而是关于如何存储你的数据。这听起来像你想存储什么可能被认为是一个稀疏矩阵,即你只想存储非空值。查看 EAV模型。这对于这样的数据非常有用,尽管它有一些主要的限制,你应该让自己知道。

表结构本质上是将列变为行。就像这样:

  CREATE TABLE terms(
term_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
termname varchar(30)NOT NULL,
termvalue varchar(100)NOT NULL,
assoc_id INT UNSIGNED NOT NULL,
CONSTRAINT FOREIGN KEY(assoc_id)REFERENCES维护者(main_id)ON DELETE CASCADE)
ENGINE = InnoDB

所以你可以随意调用你的复选框。处理它们时,会有2个插入查询 - 一个用于创建主要记录;和一个存储相关的复选框值。

 插入维护(allfields,that,must,be,collected)VALUES(1 ,2,3,4,5); 
//得到last_insert_id(),这次我们说main_id = 7

INSERT INTO mytable(termname,termvalue,assoc_id)
VALUES
('color ','blue',7),
('feeling','happy',7),
('schedule','daily',7);


Think of the form I am working on as a type of survey. It has 50+ possible conditions which can all be checked - or one. Just like any form I need to be able to post the results to a MySQL database. I am trying to figure the best method to get the input from all these checkboxes without having to create individual columns for each one (unless that is considered best practice).

So here is what I was thinking could work. Assign the checkboxes name numerically from 1-N. Then iterate through every checkbox checking it's state. Then instead of making a column for each checkbox - use one column and concatenate some kind of identifier for each.

Psuedocode

For checkbox in N:
    check state of N:
        if N is checked:
            concatenated string += idenifier(N)

Is this method a good way of doing it, or do you have other suggestions?

解决方案

Your question isn't really about your form, it's about how to store your data. It sounds like you'd like to store what might be considered a sparse matrix, ie you only want to store non-empty values. Have a look at the EAV model. It's useful for data like this though it does have some major limitations that you should make yourself aware of.

The table structure essentially makes columns into rows. Something like this:

CREATE TABLE terms(
  term_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  termname varchar(30) NOT NULL,
  termvalue varchar(100) NOT NULL,
  assoc_id INT UNSIGNED NOT NULL,
  CONSTRAINT FOREIGN KEY (assoc_id) REFERENCES maintable(main_id) ON DELETE CASCADE)
ENGINE=InnoDB

So you can call your checkboxes whatever you like. When you process them, there will be 2 insert queries- one to create the main record; and one to store the associated checkbox values.

INSERT INTO maintable(allfields, that, must, be, collected) VALUES (1,2,3,4,5);
//get last_insert_id(), we'll say main_id=7 this time

INSERT INTO mytable(termname, termvalue, assoc_id)
VALUES
('colour', 'blue', 7),
('feeling','happy', 7),
('schedule', 'daily', 7);

这篇关于从Web表单上的50多个复选框获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆