0

I have a search form that can be built dynamically by the user using code such as:

$('tr.form_row:last').clone(true).insertAfter('tr.form_row:last');

All of this is working fine, however, I would like to save the dynamically generated form html when the form is submitted. I have tried using $(#my_form').html() but this doesn't appear to have the dynamic content, instead, it's just the default html before customisation that is gathered.

Is there a way of getting the generated source using javascript/jquery? It would need to include the value within the input fields that the user has typed etc.

EDIT - I've included my coding so far:

This is the coding I have tried - it uses jquery form plugin and I populate a hidden form field called 'search_html':

var search_form = {
    beforeSubmit: function() {
        var search_html = '<table>';
        $('.form_row').each(function() {
            search_html += $(this).html();
        });
        search_html += '</table>';
        $("#search_html").val(search_html);
    },
    success: function(html) { 
        $('#search_output').html(html);
    } 
};  
$('#search_form').ajaxForm(search_form);

When I submit the form, HTML is passed within the hidden 'search_html' field, but it is only the first table row and doesn't have the values of the input fields populated.

4

1 回答 1

0

由于这似乎不可能以我希望/想到的方式进行,我改为用 PHP 编写代码来收集生成的表单详细信息并将其存储在数据库中。然后当用户返回表单时,我检查他们的 user_id 是否在搜索历史表中,如果是,则使用 PHP 将表单构建回原来的样子。

提交表单时的代码:

foreach($_POST["where_field"] as $key => $val){
    $stmt = $pdo->prepare('INSERT INTO dynamic_form (user_id, where_field, where_value, where_criteria) VALUES (:var1, :var2, :var3, :var4)');
    $stmt->bindParam(':var1', $user_id, PDO::PARAM_INT);
    $stmt->bindParam(':var2', $_POST["where_field"][$key], PDO::PARAM_STR);
    $stmt->bindParam(':var3', $_POST["where_value"][$key], PDO::PARAM_STR);
    $stmt->bindParam(':var4', $_POST["where_criteria"][$key], PDO::PARAM_STR);
    $stmt->execute();
}

然后在实际的表单页面上,查询数据库$user_id并循环遍历结果以构建他们拥有的表单。

于 2013-03-12T18:10:32.267 回答