简体   繁体   中英

Dynamic generated fields and saving the data

I have about 15 form fields(fieldset) which also has a large textarea(limit 4000 characters). 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). Does this design creates any issues while saving the data to MySQL database? 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. 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). Then you could remove the data within these fieldset, like $('#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.

In PHP you can iterate through the POST vars, which should be very easy. Do some print_r() for the data to see, how it looks like.

Personally, I find more practical to generate the display of info entirely in 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. The page js immediately ajax the data back from php, and formats it. 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).

Moreover, I don't use any form, only buttons and the jquery $.post() function. 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:

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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