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_开发者_StackOverflowQuickform 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>
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.
精彩评论