Home › Forums › Quform WordPress › Populate Drop Down
- This topic has 20 replies, 4 voices, and was last updated 9 years, 2 months ago by Ally.
- AuthorPosts
- February 4, 2013 at 3:02 pm #2610lawrencepepperParticipant
Hello, I need to to populate a Dropdown with values from a table in my database dynamically.
Is this possible with the form builder, if so how do I go about this. I can’t seem to se a way. The dynamic option only allows you to set a default value.
Thanks.
L
February 5, 2013 at 9:28 am #2623AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
February 5, 2013 at 4:04 pm #2652lawrencepepperParticipantHello,
I think I am close. I am using this but it is not populating the drop down.
function mytheme_dynamic_dropdown_values($form)
{
$options = array();
$dbc = mysqli_connect('localhost', 'root', 'password', 'user');
$query = "SELECT * FROM qa_vendors";
$result = mysqli_query($dbc, $query);
$data_array = array();
while ($row = mysqli_fetch_assoc($result)) {
$data_array[$row['company']] = $row['company'];
}$dropdown = $form->getElement('iphorm_1_5');
if ($dropdown instanceof iPhorm_Element_Select) {
$dropdown->setOptions($options);
}add_action('iphorm_pre_display_1', 'mytheme_dynamic_dropdown_values');
}
In the form builder I have one option set for the dropdown as it will not et me add the element without an option. Is this the correct way to set up the form for this code to work?
L
February 5, 2013 at 4:21 pm #2655lawrencepepperParticipantI noticed some errors in the above code, fixed it but it still is not working…
{
$options = array();
$dbc = mysqli_connect('localhost', 'root', 'password', 'user');
$query = "SELECT * FROM qa_vendors";
$result = mysqli_query($dbc, $query);while ($row = mysqli_fetch_assoc($result)) {
$options[$row['company']] = $row['company'];
}$dropdown = $form->getElement('iphorm_1_5');
if ($dropdown instanceof iPhorm_Element_Select) {
$dropdown->setOptions($options);
}add_action('iphorm_pre_display_1', 'mytheme_dynamic_dropdown_values');
}
February 5, 2013 at 4:26 pm #2656lawrencepepperParticipantOK I think I am going crazy, found some more errors… Had my connect statement wrong, eiether way still not working…
function mytheme_dynamic_dropdown_values($form)
{
$options = array();
$dbc = mysqli_connect('localhost', 'user', 'paswword', 'qa');
$query = "SELECT * FROM qa_vendors";
$result = mysqli_query($dbc, $query);while ($row = mysqli_fetch_assoc($result)) {
$options[$row['company']] = $row['company'];
}$dropdown = $form->getElement('iphorm_1_5');
if ($dropdown instanceof iPhorm_Element_Select) {
$dropdown->setOptions($options);
}add_action('iphorm_pre_display_1', 'mytheme_dynamic_dropdown_values');
}
February 6, 2013 at 12:17 pm #2669AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
February 6, 2013 at 2:52 pm #2681lawrencepepperParticipantOK,
Used the code and tested it. It returns:
Array ( [0] => Array ( [label] => UAB [value] => UAB ) [1] => Array ( [label] => Carleton [value] => Carleton ) [2] => Array ( [label] => Kinsley Inc. [value] => Kinsley Inc. ) [3] => Array ( [label] => Rumsey Eelectric [value] => Rumsey Eelectric ) [4] => Array ( [label] => Cooper Power Division [value] => Cooper Power Division ) [5] => Array ( [label] => Allied Electronics [value] => Allied Electronics ) [6] => Array ( [label] => Dave Inc. [value] => Dave Inc. ) [7] => Array ( [label] => KBI [value] => KBI ) [8] => Array ( [label] => Large Systems [value] => Large Systems ) [9] => Array ( [label] => Ace Hardware [value] => Ace Hardware ) )
BUT, it is still not showing up in the dropdown on the form.
February 6, 2013 at 2:56 pm #2682AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
February 6, 2013 at 3:00 pm #2683lawrencepepperParticipantOoops. Fixed that but now the page displays blank.
I am using the code:
function enterreport_dynamic_dropdown_values($form)
{
$options = array();
$dbc = mysqli_connect('localhost', 'xxx', 'xxx', 'qa');
$query = "SELECT * FROM qa_vendors";
$result = mysqli_query($dbc, $query);while ($row = mysqli_fetch_assoc($result)) {
$options[] = array('label' => $row['company'], 'value' => $row['company']);
}$dropdown = $form->getElement('iphorm_1_5');
if ($dropdown instanceof iPhorm_Element_Select) {
$dropdown->setOptions($options);
}
}
add_action('iphorm_pre_display_1', 'enterreport_dynamic_dropdown_values');
February 6, 2013 at 3:04 pm #2684AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
February 6, 2013 at 3:10 pm #2688lawrencepepperParticipantGetting the error:
Fatal error: Call to undefined function mysqli_connect() in C:\Users\Administrator\Documents\Websites\QAData\wp-content\themes\dejavu\functions.php on line 46
Not sure why it is failing now. When I tested just the query and array it returned results fine.
L
February 6, 2013 at 3:16 pm #2689AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
February 6, 2013 at 3:18 pm #2690lawrencepepperParticipantWas thinking same thing. Just fixed. I did not have the Mysqli dll extension enabled in my php.ini file.
Works like a champ now.
Thanks so much for all the help!
L
February 6, 2013 at 3:32 pm #2691lawrencepepperParticipantHave a small issue that has come up…
I am using this code to send some of the form data to a script. One of the data fields is the dropdown box from above that is being dynamically filled.
function mytheme_post_to_another_script($form)
{
// Rewrite the post variables
$post = array(
'vendor' => $_POST['iphorm_3_3'],
'newvendor' => $_POST['iphorm_3_7'],
'rep' => $_POST['iphorm_3_8'],
'delete' => $_POST['iphorm_3_11'],
);// Create a new cURL resource
$ch = curl_init();// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, 'http://test.gen-techno.com/scripts/vendorupdate.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));// Grab URL and pass it to the browser
curl_exec($ch);// Close cURL resource, and free up system resources
curl_close($ch);
}
add_action('iphorm_post_process_3', 'mytheme_post_to_another_script', 10, 1);
Now the script no longer works, what is the name of the field string that is now being passed of the drop down, I believe it is not being passed as defined above any longer…
February 6, 2013 at 3:39 pm #2692AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
- AuthorPosts
- You must be logged in to reply to this topic.