QuickForm - dynamically add/del form elements question

From: Date: Tue, 26 Aug 2003 02:29:17 +0000
Subject: QuickForm - dynamically add/del form elements question
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-20506@lists.php.net to get a copy of this message
Hi all, I've found HTML_QuickForm in my search for a good tool to use as a base for a Dynamic Data Entry project I'm working on in my copious (ha!) spare time. I've gone over Keith Edmund's excellent tutorial on QuickForm, which is why I'd like to use this package if at all possible. Basically, I have a MySQL DB with a primary table and three secondary tables. Each record can have zero or more references to it from any of the three sub-tables. Think of it like the Music database given in the MySQL & mSQL book from O'Reilly, though specific to my needs. So I'd like to have some way to allow the user who's adding data to dynamically add Text fields (and Select fields eventually) to the data entry form as they work. I've got my basic test script up on: cotarium.org/AAS/john/test/QF-refs.php but I'm running into a couple of stumbling blocks. 1. How can I bind a button to clear a specific text element? Esp when the text element and button are in a group together? And when I don't know how many items there will be in the group? 2. Now that I know how to do that part, how do I create/delete arbitrary elements in a form? Basically, I'll just have a special group (the base one probably) which allows you to add an new element. Then each text element will have two buttons, one to clear an entry, another to delete it. Though clearing could be considered deleting of course! So far my code does allow me to create a number of entry fields, and I can get back data from them all (which is farther than I got with oohforms from phplib-7.2d) but I'm stumped on the button interaction unfortunately. Here's my current testing source code below. Thank you for any help you can give to put me on the right track here. John john " circled-A " stoffel . org ------------------------------------------------------------------- <!Doctypex HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Testing PEAR HTML_QuickForm</title> <style type="text/css"> .ok { background: green; } .warn { background: yellow; } .error { background: red; } </style> </head> <body bgcolor="#E0E0E0"> <?php require_once "HTML/QuickForm.php"; $refscnt = 2; $form = new HTML_QuickForm('refsTest', 'post'); $form->addElement('header', 'MyHeader1', 'References plain'); for ($x = 0; $x < $refscnt; $x++) { $form->addElement('text', "ref[$x]", "<B>Ref:</B>"); $form->addElement('button',"clrref[$x]", "Clear"); } $form->addElement('header', 'MyHeader', 'References grouped with buttons'); for ($x = 0; $x < $refscnt; $x++) { $refgrp[$x]['t'] = &HTML_QuickForm::createElement('text', "f$x"); $refgrp[$x]['c'] = &HTML_QuickForm::createElement('button', "clrrefg$x","Clear"); $form->addGroup($refgrp[$x], 'refgrp','<B>Ref:</B>','&nbsp;'); } $form->addElement('submit', 'btnSubmit', 'Submit'); if ($form->validate()) { # If the form validates then freeze the data $form->freeze(); $form->process('process_refs', false); } elseif ($clrref0) { echo "clrref[0]<br>\n"; } elseif ($clrref[1]) { echo "clrref[1]<br>\n"; } elseif ($clrrefg[0]) { echo "clrrefg[0]<br>\n"; } else { $form->display(); } function process_refs ($values) { echo "<pre>"; foreach ($values as $key=>$value) { if (is_array($value)) { echo "Found Sub-values for $key...<br>"; foreach ($value as $subkey=>$subvalue) { echo $subkey."=".$subvalue."<BR>\n"; } } else { echo $key."=".$value."<br>"; } } echo "</pre>"; } ?> <hr> <A href="QF-refs.php">Reload QF-refs.php</a> </body>

« previous php.pear.dev (#20506) next »