Can I put options in a select field from a database query?
dolittle - June 13, 2007 - 21:12
| Project: | Webform |
| Version: | 5.x-2.1 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | whiteowl |
| Status: | needs work |
Description
Hi,
Is it possible to write php code in the options field to automatically and dynamically populate
the list of options from a table in the database?
If it`s not possible, could you point me to another way of doing it?
What I want is that the different options will be the recent story posts on my site.
Thanks

#1
I need exactly the same thing. I have succeeded in adding the fields dynamically using hook_form_alter(). Unfortunately, I haven't been able to get Webform to accurately reproduce the dynamically generated content in the email or the submission view.
I'm very close. If anyone has other pieces to this puzzle I think we can make this work. Maybe even create a new component?
#2
Okay, I have found several other requests for this. I have pointed them all at this thread and closed them as duplicates. I will finish a preliminary hack for this soon.
#3
#4
I have finished a very preliminary hack. I have started documenting what I did, and I am providing the source code that I have developed so far. You can find all of this at:
http://www.irolo.net/drupal_webform_and_dynamic_checkboxes
I hope to polish this over the next few days.
#5
What about the Active Select module?
#6
I'm not sure that Active Select does what we need. Not only do you have to generate the selection dynamically, but you also have to save a submitted value to the database and retrieve it later.
#7
I have finished the final version of my dynamicselect component. Documentation and the file are available at:
http://www.irolo.net/drupal_webform_and_dynamic_checkboxes
In addition, a demonstration will be available for a while at my staging site:
http://immaculata.bydesign-area51.com/registration
At this point I think that I have provided enough of a framework for a developer to solve similar problems. I would like to encourage others to expand on what I have done, but I am closing this particular issue for now.
#8
Hi RoloDMonkey ,
Thanks for your tutorial and code. I have used it and ammended it to prepopulate the webform with data from the user profile table. That all works fine. However, I am having trouble getting the default value to set. Its probably really simple. I know this issue is closed, but I am not sure where else to post. Can I post my alterations here for someone to have a quick look?
#9
Hi,
I set this item to active if you don't mind, I was wondering what the status of this component is after release 1.8?
Will your dynamicselect component be part of webform components in this great module?
This way it will be possible to follow up webform development more quicker, right?
Thanks for your consideration already!
greetings,
Martijn
#10
Martijn,
The dynamicselect component that I created was only to meet my needs. I documented what I did so that other developers could create similar solutions.
I do not currently have any plans to turn the dynamicselect into a "real" webform component. I would love to see someone else build on what I did, but I simply do not have the time.
If you need a particular solution but you are not able to write it yourself, you can try the companies that provide Drupal Services or you can get bids from the Paid Drupal services forum.
#11
blairski,
Please post your code as an attached file and I will see if it is something easy to figure out.
#12
I'd certainly be up for including this as part of webform, though I'd like to wrap it into the existing select element as either a separate field or just a checkbox for "Use PHP to generate options" to reuse the existing textarea.
#13
When are you planning to implement this functionality?
I've tried webform 2.x but couldn't find it.
Thanks
#14
Yes, I've got a lot on my plate but I hope to get to this soon.
#15
RoloDMonkey,
Sorry it has take me a while to attach the file. I changed you function _dynamicselect_load_options() to bring in one of the user profile fields (counties). That works fine. I am just getting stuck on setting the default, which should be the users current county that they set in their profile.
#16
There is a beta1 release of webform 2.0.
Will the 2.0 version support this feature?
Please...
#17
No, the first release of 2.0 will not support this feature. I'm very busy with a lot of things.
Again, patches to add the feature would gladly be accepted as long as they are well-written and included in the current select.inc rather than as a another select list. This feature will only be included in the 2.x version as I'm trying to depreciate the 1.x version.
#18
Marked http://drupal.org/node/242644 as duplicate.
#19
I have downloaded the dynamicselect.zip file and looked at the code. Unfortunately, as a newbie it's beyond me as to how to make this work, or to make it into a patch.
It would be extremely useful if anyone could give some advice as to how to use this code.
#20
I can't get this patch to work... any help guys
#21
ok a little update,,
this works,, but for some reason the I can't get the selected fields to display into the submitted email..
I get all other fields but this one,,
is something missing?
#22
Marked #254169: Using hook_form_alter to change options. as duplicate.
#23
Here is a patch for select.inc that was made against 5.x-2.0. It adds a php fieldset which contains a boxes for adding php code that creates defaults and for options. In order to see the (collapsed) fieldset the user must have "use PHP for additional processing" permissions.
This patch also makes the Options box optional (since the php generated data overwrites anything in that field) but adds some error checking to make sure that there are either options or php.
Several weeks ago I looked at the differences between 5 and 6 and it looked to me like it might well apply cleanly against 6 but I have not tested it.
#24
UMM. It seems that the nesting caused an error message to show up when it should not have (claiming no data). A slightly better patch.
#25
Thanks for the patch whiteowl!
The approach in this patch seems a bit strange. Rather than evaluating the PHP when showing the form, you're evaluating when creating it. This makes it so that the select list can't contain truly dynamic values, you basically just saving a little bit of work when setting up the form initially.
This patch should be made to work on display, rather than inserting values and then letting Webform use those options as it did before.
#26
I agree that this is strange but I believe that it is not strange how you think it is strange.
Try adding this as the php Options:
return array('c'=>date('c'), 'r'=>date('r'), 'U'=>date('U'));Off topic here, but I get an error message (which I do not see the source of) about illegal choices if I do not make these associative arrays. Although the error message is different ("An illegal choice has been detected. Please contact the site administrator.") I thought it might be some sort of problem with the special characters, but I do not understand why date('U'), which is just a number, does not work. The patch only evaluates the PHP one time, but I wonder if the function is called several times and the discrepancy of the choices between these calls triggers an error which in turn generates the error message.
When you refresh the form each of these dates updates every time (at least for me).
Every time you generate the form you will see the values from the last invocation of the form in the Options box. This is because (and this is the slightly odd thing) every time the form is generated, the newly generated values are stuffed back in the database so that when the form is processed the data shows up as valid data. Without this step the data does not get entered into permanent storage. This is what you helped me with before in the issue http://drupal.org/node/254169. So the form administrator sees the historical data but the users do not!
So the summary is - yes some strangeness - no it works how you want (I think).
#27
One other thing I did not notice before is that this makes the reporting modules a bit wierd.
I am mostly interested in the table output and that works fine both for both my interest and for the dates (at least as associative arrays which renders the results to be unchanging - not the most useful case).
The odd behavior comes if you go to the results tab and click the view screen for a transaction. A new form is generated from the data, and that new form will have the current date (from the PHP) rather than the original date. This is OK if you are having your users pick a date format, but not if you wanted them to pick a specific time (One week from now, Two weeks from now, Never). This will not be specific to this date example, but to any changing data (sort of the point for using PHP in the first place). This is a wider issue and its solution will require more restructuring beyond the select.inc file.
#28
subscribe
#29
subscribe
#30
subscribe
Ok, I've seen a patch of a patch by whiteowl in #24.
Is it the full patch for the original select.inc, since there is no attached patch in #23?
#31
The full patch is in 24 as an update to the patch that as previously in 23.
#32
There is no patch in #23 and those "-" and "+" signs at the beginning of the lines just don't make sense if I apply #24 on the original select.inc file (I'm using webform 5.x-2.1.1). I've just reversed the logic: all "-" became "+" and now it works, but the result is not so stable.
#33
Hi Madjoe, Could you file your patch please?
Thanks a lot in advance,
greetings,
Martijn
#34
Since I don't know which version of webform do you use, here's the entire select.inc in the archive for 5.x-2.1.
It still have some weird bugs and it's not that stable so this code really does need work, and I'm playing with it right now.
@whiteowl: Is it possible that this does not work as select options with this PHP addition to a select component:
<?php$m = db_query("SELECT nid, title FROM table");
$f = array();
while ($nids = db_fetch_object($m)) {
$obj = check_plain($nids->nid) . "";
$obj_name = check_plain($nids->title);
$f[$obj] = $obj_name;
}
return $f;
?>
I've checked the same PHP code on page.tpl.php and I can see the array elements by doing print_r ($f); but I have problems with creating options for my select box by using this method.
Could you please check my select.inc... did I miss something with your patch?
If I try to Submit this custom PHP code while creating a new select component, and go to see the form, I could only see "select..." option with no other options inside of my "php select box", and if I refresh the same page, I can't see that select box anymore - it just vanishes somehow. Maybe we should escape double quotes before storing custom PHP code like this to the database?
#35
I've created a function func() in my template.php that returns array("A", "B", "C").
If I create a select component, this is what is stored in the database within extra field:
a:2:{s:3:"php";a:2:{s:10:"phpoptions";s:14:"return func();";s:10:"phpdefault";s:0:"";}s:6:"aslist";s:1:"Y";}After I visit the form for the first time, I could only see "select..." as an option in a select component.
After my visit to the form this is the content of the same extra field, but the select component is not visible in my future page visits:
pre_a:3:{s:3:"php";a:2:{s:10:"phpoptions";s:14:"return func();";s:10:"phpdefault";s:0:"";}s:6:"aslist";s:1:"Y";s:5:"items";s:5:"AB
C";}
Take a look at the prefix "pre_" that is the default prefix for my database tables?! The regular "Default value" becomes "pre_".
#36
subscribe
#37
subscribing
#38
-subscribe-
#39
To re-iterate, the approach in this patch is incorrect and need to be rewritten before it is included in the final project. Instead of pulling in the options when configuring the element, the options should be pulled in when displaying the element (or viewing results). This patch needs to be fixed before it's included in Webform.
#40
subscribing
#41
I am probably way off here, but:
You can put a token in the options field. I have experimented where I put %get[model] in the options field, which will show "blah-blah" as the (one) select option if URL to the form is /mywebform?model=blah-blah.
I know nothing about "token creation" but would there be a way to create a token whose value contains the result of a query to the database that brings back the stuff you want to show here? Then you could just put that token in the options field?
BTW this is all for Webform 6.x-2.3
I tried RoloDMonkey's method above but am lost because things have change so much from 5.x to 6.x-2
#42
I put together a 5.x module for dynamic selects. None of the other patches fit the bill for what I needed. It renders the options at display time. I'll gladly port it to 6.x if there is any interest. I currently have the module hosted at http://github.com/ngmaloney/dynamic_select/tree/master
#43
Wow that's great. I would greatly appreciate a port to 6 when you get a chance. I am working on a D6 project now that needs this.
#44
+1 D6, subscribing
#45
+1 for D6!
#46
D6 pls
Need it for emails of all employees in company and don't want to show email addresses on the site (neither displayed nor HTML code)
#47
I'm kind of desparate to get a dynamically created list of checkbox choices on a webform for a project I am working on (D6 and Webform 6.x-2.3). Wondering if there is some other option. Could I add the check boxes via hook_form_alter with a dbquery in there someplace or would that not work?
In a post above I was also wondering if somehow a token could be created (not sure how to create tokens) that could be put as default for this field.
If all else fails I guess I could forget abotu using webform for this particular form, create a content type for it instead and use messaging to email it.
Any direction anyone can give me would be appreciated. ;-)