简体   繁体   English

动态生成字段并保存数据

[英]Dynamic generated fields and saving the data

I have about 15 form fields(fieldset) which also has a large textarea(limit 4000 characters). 我大约有15个表单字段(fieldset),其中也有很大的textarea(限制4000个字符)。 I have to generate dynamically these set of fields when the button is clicked. 单击按钮时,我必须动态生成这些字段集。 I have to accommodate to create these fields n number of times(no limit). 我必须适应n次创建这些字段(无限制)。 Does this design creates any issues while saving the data to MySQL database? 在将数据保存到MySQL数据库时,这种设计是否会产生任何问题? Also What is best method to generate the fields dynamically and validate them? 还有什么是动态生成字段并进行验证的最佳方法? I appreciate any input. 感谢您的投入。

Thank you. 谢谢。

For the jQuery solution I did something similar once. 对于jQuery解决方案,我曾经做过类似的事情。 If you've got some fieldset, you could take $('#yourfieldset').html() and append it to the last one, which is existing (so at first the first one). 如果您有一些字段集,则可以使用$('#yourfieldset')。html()并将其附加到最后一个存在的字段上(因此第一个是第一个)。 Then you could remove the data within these fieldset, like $('#yourfieldset input').val(""). 然后,您可以删除这些字段集中的数据,例如$('#yourfieldset input')。val(“”)。 So you've got the duplicating, and you could also create a little function to duplicate it several times. 这样您就可以进行复制,并且还可以创建一个小函数来将其复制几次。

For the saving, you could change the name-attributes of the fieldset, which you are copying. 为了保存,您可以更改要复制的字段集的名称属性。 Something like: 就像是:

$('#yourfieldset input').each(function() {
 $(this).attr("name",  $(this).attr("name") + "_" + counter);
});

You need that counter as variable as global of course, to count the fieldsets up. 当然,您需要将该计数器作为全局变量来计数字段集。 To remove a fieldset, its also possible to just give the function this counter or lets call it rownumber. 要删除字段集,也可以只给该函数提供此计数器或让其称为行号。 So you could delete it with $('#yourfieldset["id$=_15"]').remove() because it looks for the id attribute with the specific number and removes that fieldset. 因此,您可以使用$('#yourfieldset [“ id $ = _ 15”]')。remove()删除它,因为它会查找具有特定编号的id属性并删除该字段集。

In PHP you can iterate through the POST vars, which should be very easy. 在PHP中,您可以遍历POST变量,这很容易。 Do some print_r() for the data to see, how it looks like. 做一些print_r()来查看数据,看起来如何。

Personally, I find more practical to generate the display of info entirely in js/jquery. 就个人而言,我发现更实用的方法是完全在js / jquery中生成信息显示。 This way, you don't have to handle the display in both languages. 这样,您不必处理两种语言的显示。 php sends an "empty" shell, with tiles and some static text, and a js file. php发送一个“空” shell,其中包含图块和一些静态文本以及一个js文件。 The page js immediately ajax the data back from php, and formats it. 页面js立即将数据从php移回ajax,并将其格式化。 If you do things carefully, you can reuse the procedure easily. 如果您仔细地做事,则可以轻松地重用该过程。 I have gone as far as create types for each table fields, which tell how to display the field (id: "hidden", name: "text", county: "select", active: "checkbox", etc). 我已经为每个表字段创建了类型,它们告诉如何显示字段(id:“隐藏”,名称:“ text”,县:“ select”,活动:“ checkbox”,等等)。

Moreover, I don't use any form, only buttons and the jquery $.post() function. 而且,我不使用任何形式,仅使用按钮和jquery $ .post()函数。 This allows to build the post data easily with something like that: 这样可以使用以下类似的方法轻松构建发布数据:

$('table.VirtualFormTable').delegate('.update, .create', 'click', function() {  // .update and .create are button's class on each row.

    var $this = $(this);
    var $row = $this.closest('tr');
    params = {};
    $('input, select', $row).each(function () {
        if ( this.type == 'checkbox' ) params[this.name] = this.checked;
        params[this.name] = this.value;
    });

    if ( validateParams(params) ) {
        $.post(url, params, function (data) { $row.removeClass('updatePending'); });
    }

});

it's quite easy to loop each row that has changed, and build an "array" of params which looks like that: 循环更改的每一行很容易,并构建一个如下所示的参数“数组”:

{[ 
    0: {param1: '...', param2: '...'},
    1: {param1: '...', param2: '...'}
]}

then in php you can do something like that: 然后在php中,您可以执行以下操作:

$fields = str_replace(array('true', 'false'), array('1', '0'), $_POST);  // transform checkboxes
$whereArray = array_slice($fields, 0, 1); // table_index in first position
myDbTools::insertOrUpdate('table', $fields, $whereArray);

with a 3 part template like that, you need to write 2 lines to create a complete form based on a table. 使用一个由3部分组成的模板,您需要编写2行以基于表格创建完整的表单。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM