I have seen some forms which dynamically add fields as needed based on user input.
For example, a question (e.g. list box) might ask: "Do you smoke?"
If the user selects "No", the form does not change. However, if the user selects "Yes" (from the listbox), the form automatically inserts a new row into the form with a new question and listbox field:
"What do you smoke?"
The user can then select an entry from the new listbox: "tobacco", "marijuana", or "crack".
This strategy allows a form to stay as small as possible by omitting entry fields unless they are necessary.
Question 1: Where could I find PHP code that would do this or what approach would you recommend?
(I want the new row to be inserted via PHP into the HTML - not just "hiding" in the existing html.)
Question 2: Does this data entry method have a name?
Dynamically Added Form Fields
Moderator: General Moderators
-
michaeldallas
- Forum Newbie
- Posts: 1
- Joined: Tue Mar 25, 2003 11:23 pm
-
luisquijada
- Forum Newbie
- Posts: 2
- Joined: Wed Mar 26, 2003 2:07 am
That is all you need.
Your select list must be placed at a form
thar recharges the page. Change the "$_REQUEST" variables and go on!
// function that output the list.
function do_submit_picklist ($name, $_opts, $_vals, $selected_val) {
print('<SELECT NAME="'.$name.'" ');
print('ONCHANGE="this.form.submit();"');
print('>\n');
for ($i=0;$i<sizeof($_opts);$i++) {
print('<OPTION VALUE="'.$_vals[$i].'"');
if ($selected_val==$_vals[$i]) print(' SELECTED');
print('>'.$_opts[$i].'</OPTION>\n');
}
print('</SELECT>\n');
}
// if smoke do the list... into a table cell?
if ($_REQUEST['do_you_smoke']=='yes') {
do_submit_picklist('what_do_you_smoke',
array('tobacco','marijuana','crack'),
array('t','m','c'), $_REQUEST['what_do_you_smoke']);
}
Your select list must be placed at a form
thar recharges the page. Change the "$_REQUEST" variables and go on!
// function that output the list.
function do_submit_picklist ($name, $_opts, $_vals, $selected_val) {
print('<SELECT NAME="'.$name.'" ');
print('ONCHANGE="this.form.submit();"');
print('>\n');
for ($i=0;$i<sizeof($_opts);$i++) {
print('<OPTION VALUE="'.$_vals[$i].'"');
if ($selected_val==$_vals[$i]) print(' SELECTED');
print('>'.$_opts[$i].'</OPTION>\n');
}
print('</SELECT>\n');
}
// if smoke do the list... into a table cell?
if ($_REQUEST['do_you_smoke']=='yes') {
do_submit_picklist('what_do_you_smoke',
array('tobacco','marijuana','crack'),
array('t','m','c'), $_REQUEST['what_do_you_smoke']);
}