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

发布于 2024-11-26 18:17:12 字数 3204 浏览 4 评论 0原文

如何使用 HTML_Quickform 动态添加表单元素。我知道如何使用纯 HTML 表单元素和 Javascript 来完成此操作(请参阅下面的代码)。我想知道使用 HTML_Quickform 是否容易做到这一点。我仍在查看此网站: http ://devzone.zend.com/article/2699-Generate-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>

How do I dynamically add form elements using HTML_Quickform. I know how this is done using plain HTML form elements and using Javascript (see code below). I wanted to know if this is easy to do using HTML_Quickform. I am still looking at this site: http://devzone.zend.com/article/2699-Generating-and-Validating-Web-Forms-With-PEAR-HTML_QuickForm. HTML_Quickform lets you easily add validation rules. I began creating a similar HTML_Quickform code - but I could not figure out how to dynamically add form elements using 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; 
?>

The code for the plain HTML with Javascript is as follows:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2024-12-03 18:17:12

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

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

Unless I'm misunderstanding your question, you can't. You're using HTML_Quickform to generate HTML on the server. Once you output $formsource, your php code (and thus HTML_Quickform) cease to execute, so you wouldn't be able to programmatically add anything with HTML_Quickform. So, once you output the HTML to the browser, you can only manipulate your HTML-based form with JavaScript.

One workaround would be to create some HTML form fragments with HTML_Quickform and inject those into your HTML form with AJAX.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文