如何使用PEAR的HTML_Quickform包动态添加表单元素

时间:2011-07-25 18:34:48

标签: php forms add dynamic elements

如何使用HTML_Quickform动态添加表单元素。我知道如何使用纯HTML表单元素和使用Javascript(请参阅下面的代码)。我想知道使用HTML_Quickform这是否容易。我仍然在看这个网站:http://devzone.zend.com/article/2699-Generating-and-Validating-Web-Forms-With-PEAR-HTML_QuickForm。 HTML_Quickform允许您轻松添加验证规则。我开始创建一个类似的HTML_Quickform代码 - 但我无法弄清楚如何使用HTML_Quickform动态添加表单元素:     

// Our Default Form Options
$opts = array('size' => 20, 'maxlength' => 255, 'id' => 'name1', 'class' => 'clonedInput' );

$form->addElement('text', 'first_name', 'First Name', $opts);
$form->addElement('button','btnAdd', 'Add another name', array('id' => 'btnAdd'));
$form->addElement('button','btnDel', 'Remove Name', array('id' => 'btnDel') );

// Submit button
$form->addElement('image', 'register', 'images/continue.gif');

// Define filters and validation rules
$form->applyFilter('first_name', 'trim');
$form->addRule('first_name', 'Please enter your First Name', 'required', null, 'client');

$formsource = $form->toHtml();

echo $formsource; 
?>

使用Javascript的纯HTML代码如下:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    $('#btnAdd').click(function() {
        var num     = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
        var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added

        // create the new element via clone(), and manipulate it's ID using newNum value
        var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

        // manipulate the name/id values of the input inside the new element
        newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);

        // insert the new element after the last "duplicatable" input field
        $('#input' + num).after(newElem);

        // enable the "remove" button
        $('#btnDel').attr('disabled','');

        // business rule: you can only add 5 names
        if (newNum == 5)
            $('#btnAdd').attr('disabled','disabled');
    });

    $('#btnDel').click(function() {
        var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
        $('#input' + num).remove();     // remove the last element

        // enable the "add" button
        $('#btnAdd').attr('disabled','');

        // if only one element remains, disable the "remove" button
        if (num-1 == 1)
            $('#btnDel').attr('disabled','disabled');
    });

    $('#btnDel').attr('disabled','disabled');
});
</script>


<form id="myForm">
    <div id="input1" style="margin-bottom:4px;" class="clonedInput">
        Name: <input type="text" name="name1" id="name1" />
    </div>

    <div>
        <input type="button" id="btnAdd" value="add another name" />
        <input type="button" id="btnDel" value="remove name" />
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

除非我误解你的问题,否则你不能。您正在使用HTML_Quickform在服务器上生成HTML。输出$formsource后,您的PHP代码(以及HTML_Quickform)将停止执行,因此您将无法以编程方式添加任何HTML_Quickform。因此,一旦将HTML输出到浏览器,您就只能使用JavaScript操作基于HTML的表单。

一种解决方法是使用HTML_Quickform创建一些HTML表单片段,并使用AJAX将它们注入到HTML表单中。