Forums

Generating form fields based on input

h3g 20 Jun, 2008
I've setup a testing site to experiment with ChronoForms. I'm trying to do the following (example😉):

- Form 1: ask the user how many fingers he/she has.
- Form 2: create a field for every finger the user has.

Assuming someone has 10 fingers, form 2 should show 10 fields. These could be just text, or selectboxes, or whatever input type I feel fits my need. Displaying is easily done using a loop in PHP, no problems so far. Every field gets it's own name, finger[1], finger[2], etc.

Trouble is, Chrono doesn't know the names of the fields, because they where created by PHP on requesting the page.

Is there any way to make this work? I'm looking for a very scalable solution that works up to, say, 50 to 100 fields.

I can imagine a 'hack' where I insert a piece of HTML that contains 10 hidden fields that have the same name as the fields that I dynamically generate (so, a list from finger[1] to finger[10]) so Chrono always records 10 fields and allows me to select validation for them. Then have the user's browser sort out which fields are duplicate and which value to POST.

But before I will try this hack, I'd like to know if there are any better and scalable solutions.

Hope my question is clear😉
Thanks in advance.
GreyHead 21 Jun, 2008
Hi h3g,

Sounds as though you are on the right track. I do this kind of coding in ChronoForms quite often,

You don't need ten hidden fields - if you are using an array variable like
finger[]
then
<input type='hidden' name='finger' value='' />
will work OK except that ChronoForms won't know to unpack the array before saving it in the database so you'll need to do that manually.

You could probably do the form expansion in JavaScript. Just create all ten fields and hide them. Then use the JavScript to un-hide the correct number and turn the validation on.

Bob
h3g 21 Jun, 2008
Hi Bob,

Thanks for your reply. I will try this on monday when I'm at work again🙂
h3g 23 Jun, 2008
Could you give me a hint on how to use these variables in an e-mail template and how to assign validation to them in case I decide not to use javascript to show/hide the fields? (if it's possible to do that)

I've tried including {finger} which results in having the full array printed in the e-mail (comma seperated). Now I'd like to do a little formatting on that or perhaps make decisions based on the information posted.

I tried forming a message using PHP in "On Submit code - before sending email":
<?php

foreach($_POST['vingers_text'] as $key => $value) {
  $_POST['compiled_message'] .= '<p>'.$key.' '.$value.'</p>';
}

?>

and then tried to use this var in the e-mail template using {compiled_message}, but the var isn't replaced before the e-mail is sent.


Concerning validation, adding finger[1], finger[2], etc to the validation doesn't seem to help. Perhaps I tried to cut a corner here😉 I'm using J1.5.3 and the Mootools lib.

[edit]
I also tried to create a table for this form, but I think it's struggling with the dynamically created fields:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near

'Array NOT NULL , PRIMARY KEY ( `cf_id` )) ENGINE = MYISAM' at line 1

SQL=CREATE TABLE `jos_chronoforms_2`
(`cf_id` INT( 11 ) NOT NULL AUTO_INCREMENT,
`uid` VARCHAR(255) NOT NULL,
`recordtime` TEXT NOT NULL,
`ipaddress` TEXT NOT NULL,
`name` LONGTEXT NOT NULL,
`email` LONGTEXT NOT NULL,
`vinger` Array NOT NULL,
PRIMARY KEY ( `cf_id` ))
ENGINE = MYISAM;')


I don't think 'Array' is a valid MySQL datatype, but I have no idea how chronoforms determines this.

Does this mean that posting values to the database cannot be done by ChronoForms but should be done 'manually'?
GreyHead 23 Jun, 2008
Hi h3g,

First off there is a sequence problem. The flow of ChronoForms* is:

1) substitute field names in $html_message

2) run the 'OnSubmit before email' code

3) evaluate $html_message

So the {vinger} substitution has been done before your Onsubmit code.

However you can still use
<?php echo $_POST['compiled_message']; ?>
in your template because that will be evaluated after the OnSubmit code.

Second. For validation, turn it ON in the Forms Manager but then add "class='required'" etc. directly to the html in your php loop.

Third. You are correct - Array isn't a valid MySQL type. Create a text field and use the PHP implode/ explode functions to convert the array to or from a string.

Bob

* I'm not sure that this is ideal but it's how it works unless you hack the code!
This topic is locked and no more replies can be posted.