This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Populating a drop down list with data from MySQL/PHP


  1. Josh
    Member

    I'm looking for a way to implement this standard php code into the dropdown list of a couple of forms:

    [code]
    <? php
    $sql="SELECT id,element_1 FROM ap_form_2";
    $result=mysql_query($sql);

    $options="";

    while ($row=mysql_fetch_array($result)) {

        $CompanyID=$row["id"];
        $CompanyName=$row["element_1"];
        $options.="<OPTION VALUE=\"$CompanyID\">".$CompanyName.'</OPTION>';
    }
    ?>
    [/code]

    Looking through the code, it looks like the simplest way to do this would be to create a new element_type; say "dropdown_company" in /includes/entry_functions.php, and then change the dropdown on my form to that type, but I am open to suggestions.

    Thanks in advance
    Josh

    Posted 15 years ago #
  2. yuniar

    I must say this wont be pretty simple.

    You need to modify display_select() function in "includes/view-functions.php" to get the options from a query like what you did above.

    Then you need to modify the ap_form_x table structure, particularly the column which holds the value of the dropdown. Currently the field type is INT. I suppose you need to change it to VARCHAR or TEXT

    Few others adjusment might be needed.


    MachForm Founder

    Posted 15 years ago #
  3. Josh
    Member

    To address the datatype: I fully intend to still use the dropdown list to populate a number as the option value, to maintain compatability (this will be the thankfully already generated unique id of "id" from the form "ap_form_2")

    I'm going to look into the includes/view-functions.php shortly and see how it works..
    Are you suggesting that I would need to modify display_select() as well as some part of entry_functions.php?

    Unfortunately, I just started with MachForms, so I don't yet have a handle on the overall flow of the engine (though, I am impressed with what I understand so far)

    Thanks

    Posted 15 years ago #
  4. yuniar

    Yes, you will definitely need to modify display_select() function. This is the function which handles the drop down.


    MachForm Founder

    Posted 15 years ago #
  5. Josh
    Member

    Ok; I successfully created what I want by editing the function 'display_select($element)' in /includes/view-functions.php (about line 595)

    However; this changes all dropdowns, which is no good.

    I would love to know how to take the work I've done, and create a new function: 'display_select_company($element)', then be able to apply that to the form element.

    I know that it would be a huge pita to create the admin GUI for this, so I would rather just be able to set the element_type manually in the database, then have it use my custom element type without breaking anything else :)

    Thanks for all your help

    Posted 15 years ago #
  6. Josh
    Member

    I was doing testing on the wrong form element; I can do exactly what I was hoping to.

    For the record:

    I opened /includes/view-functions.php, went to about line 595, and copied the entire function "display_select", then duplicated it.
    I renamed this function to "display_select_company", and made the following change:

    Replaced

    $has_default = false;
    foreach ($element->options as $option){

    if($option->is_default){
    $selected = 'selected="selected"';
    $has_default = true;
    }else{
    $selected = '';
    }

    if(!empty($element->populated_value['element_'.$element->id]['default_value'])){
    $selected = '';
    if($element->populated_value['element_'.$element->id]['default_value'] == $option->id){
    $selected = 'selected="selected"';
    }
    }

    $option_markup .= "<option value=\"{$option->id}\" {$selected}>{$option->option}</option>\n";
    }

    if(!$has_default){
    if(!empty($element->populated_value['element_'.$element->id]['default_value'])){
    $option_markup = '<option value=""></option>'."\n".$option_markup;
    }else{
    $option_markup = '<option value="" selected="selected"></option>'."\n".$option_markup;
    }
    }

    with this

    $sql="SELECT id,element_1 FROM ap_form_2";
    $result=mysql_query($sql);
    while ($row=mysql_fetch_array($result))
    {
    $CompanyID=$row["id"];
    $CompanyName=$row["element_1"];
    $option_markup .="<OPTION VALUE=\"$CompanyID\">$CompanyName</OPTION>";
    }

    Then, I modified the sql row as follows:

    UPDATE asterisk.ap_form_elements SET element_type = 'select_company' WHERE ap_form_elements.form_id =3 AND ap_form_elements.element_id =5 LIMIT 1 ;

    And tada! It works!

    Posted 15 years ago #
  7. Josh
    Member

    Hmmmm... ran into a snag: When I choose an entry from the dropdown, and submit: it comes through as Zero.

    I made sure that the html looks right:

    <select class="element select small" id="element_6" name="element_6">
    <OPTION VALUE="1234">Test One</OPTION>
    <OPTION VALUE="105">Test Two</OPTION>
    <OPTION VALUE="106">Test Three</OPTION>
    <OPTION VALUE="107">Test Four</OPTION>
    <OPTION VALUE="200">test five</OPTION>
    </select>

    Any input as to why this might be happening?

    Posted 15 years ago #
  8. Josh
    Member

    Ok, so I worked backwards, assuming there was a form validation routine, and found it :)

    file: /includes/post-functions.php , ~ line 624
    I found the elseif for 'select', and copied it, then just modified the element type, adding the new element_type to the list on line 643.


    }elseif ('select_company' == $element_type){ //Drop Down for Company; Added by [Josh]

    if($element_info[$element_id]['is_required']){
    $rules[$element_name]['required'] = true;
    }

    $target_input[$element_name] = $element_data;
    $validation_result = validate_element($target_input,$rules);

    if($validation_result !== true){
    $error_elements[$element_id] = $validation_result;
    }

    //save old data into array, for form redisplay in case errors occured
    $form_data[$element_name]['default_value'] = $user_input[$element_name];

    //prepare data for table column
    $table_data[$element_name] = $user_input[$element_name];

    And, form submission works perfectly now.

    Note: I have documented as much as possible, because I found a few people asking the question of how to populate a dropdown with data from another form, but noone had answered it. Hopefully, this will allow others to duplicate my work, and move forward with Machforms.

    It's a great abstraction of the forms code, and definitely one that I will use again.

    Posted 15 years ago #
  9. mig2000
    Member

    Josh,

    I am trying to start doing something like what you have done. Is it possible to share your thoughts with us? Actually I do see what you have done here but what is the final steps. I would greatly appreciate it.

    Thanks
    Alex

    Posted 15 years ago #
  10. Josh
    Member

    The final steps are pretty much laid out on here; 3 separate things need to be done in order to create a new element_type

    1) Modify /includes/view-functions.php as shown above; basically copying an existing function, modifying it, and then adding it into the file. Pick your element name at this point as well; it has to be the same everywhere (i used "select_company" to keep it consistent)

    2) Modify /includes/post-functions.php as shown; this allows the data to be processed properly once it's submitted.

    3) Manually change the element type of your form via SQL (an example is shown above)

    Now; one major thing to be aware of: Since I have not detailed any steps on modifying the admin panel (Not needed for my purposes), once you change the dropdown type, you will no longer be able to edit your form in the admin interface.
    The best sequence, assuming you are ok with that, is to create all your forms, set them up fully, choosing plain dropdowns with no entries as your element, and then go into the database and change the "select" element type to "select_whatever".
    If you need to use the admin interface again later, change the element type back to "select"

    Assuming you followed the modifications above, your new form will work in both the view, as well as the embedded.

    Note: I know it would be a fair amount of coding work, but I think it would be a killer feature to have an "external data" dropdown box element type, even if it just pulled entries from another form, like mine is doing here :)

    Posted 15 years ago #
  11. mig2000
    Member

    Many Thanks for your note. The only area I am confused is the first 2 steps. You are refering to the above and there are few entries for the codes. I am not sure which one you are refering to. Can you please drop the final text after my post? I really would appreciate that.

    Also do yo have a final link so we can see what you have done?

    Thanks

    Posted 15 years ago #
  12. mig2000
    Member

    So I have a drop down field called Element_11 in form called ap_form_2. The form that contains my courses is called ap_form_3. I need to get the Course name from forms_3 to be a look up take for Element_11 in form2.

    I went ahead and modified the Post_function and View_Function per above. Instead of company I have course - I need to know the followings:

    1- When you refer to Element_1, is that in the form_2 or form_3 in my case?
    2- Where do you do UPDATE asterisk.ap_form_elements SET element_type = 'select_company' WHERE ap_form_elements.form_id =3 AND ap_form_elements.element_id =5 LIMIT 1 ?
    3- Kind of confused how the form_2 knows to look up in form_3 when I have the field selected?

    Many thanks in advance for your help.

    Alex

    Posted 15 years ago #
  13. Josh
    Member

    Granted, my code is a little specific to my situation, so I will try to answer your questions:

    1 - I assume you are referring to $sql="SELECT id,element_1 FROM ap_form_2";
    In your case, it would be
    $sql="SELECT id,element_1 FROM ap_form_3";
    Now, this assumes the data you want to pull from is element_1 from ap_form_3

    This code is pulling in both the unique number from the entries in that form, as well as the name, assuming the name is element_1.
    You may not even need the id, depending on how you want to do it.

    2 - Basically, all you would have to do in your particular situation is go into the database, into ap_form_elements, and modify the entry that is form_id of 2, element_id of 11, and change "select" to "select_course"

    3 - form_2 doesn't actually know to look it up; my method creates a new type of dropdown box that already has the data from another table in it.
    If you are talking about the end result, as opposed to how it works; When I choose one of the companies from the dropdown and submit, it saves the ID of that company into the entry of form 3 (I put ID so I could reference them both later in another script)

    Posted 15 years ago #
  14. mig2000
    Member

    Again, you have been great. I did exactly what you said and I get an error. Let me add that your situation is exactly what I am trying to do . All I need is the course name from form_3 to be assigned to the element_11 on the form_2:

    Here is what I have done so far.
    New added function in the View_function:

    function display_select_course($element){
    //check for error
    $error_class = '';
    $error_message = '';
    $span_required = '';
    $guidelines = '';

    if(!empty($element->is_error)){
    $error_class = 'class="error"';
    $error_message = "<p class=\"error\">{$element->error_message}</p>";
    }

    //check for required
    if($element->is_required){
    $span_required = "<span id=\"required_{$element->id}\" class=\"required\">*</span>";
    }

    //check for guidelines
    if(!empty($element->guidelines)){
    $guidelines = "<p class=\"guidelines\" id=\"guide_{$element->id}\"><small>{$element->guidelines}</small></p>";
    }

    $option_markup = '';

    // New add for the table
    $sql="SELECT id,element_1 FROM ap_form_3";
    $result=mysql_query($sql);
    while ($row=mysql_fetch_array($result))
    {
    $courseID=$row["id"];
    $courseName=$row["element_1"];
    $option_markup .="<OPTION VALUE=\"$courseID\">$courseName</OPTION>";
    }
    // End of the new table lookup

    $element_markup = <<<EOT
    <li id="li_{$element->id}" {$error_class}>
    <label class="description" for="element_{$element->id}">{$element->title} {$span_required}</label>
    <div>
    <select class="element select {$element->size}" id="element_{$element->id}" name="element_{$element->id}">
    {$option_markup}
    </select>
    </div>{$guidelines} {$error_message}

  15. EOT;

    return $element_markup;
    }

    -----------------------
    Add the following to post_function

    }elseif ('select_course' == $element_type){ //Drop Down for Course; Added by [Alex]

    if($element_info[$element_id]['is_required']){
    $rules[$element_name]['required'] = true;
    }

    $target_input[$element_name] = $element_data;
    $validation_result = validate_element($target_input,$rules);

    if($validation_result !== true){
    $error_elements[$element_id] = $validation_result;
    }

    And the last part, I changed the select to select_course in the database base and I get this error.

    EOT; return ; } //Dropdown function display_select(Object){ //check for error = ''; = ''; * = ''; = ''; if(!empty()){ = 'class="error"'; = "

    "; } //check for required if(1){ * = "*"; } //check for guidelines if(!empty()){ = "

    "; } 123456 = ''; = false; foreach (Array as ){ if(){ = 'selected="selected"'; = true; }else{ = ''; } if(!empty(Array['element_'.11]['default_value'])){ = ''; if(Array['element_'.11]['default_value'] == ){ = 'selected="selected"'; } } 123456 .= " "; } if(!){ if(!empty(Array['element_'.11]['default_value'])){ 123456 = ''." ".123456; }else{ 123456 = ''." ".123456; } } = <<

    Keep in mind that 123456 is the record in the form_3 that is supposed to be selected.

    Many thanks.

Posted 15 years ago #
  • mig2000
    Member

    I am not sure if I am on the same page.

    The form_3 has three fields:

    Element_1 = Courseid
    Element_2 = Course description
    Element_3 = Price

    The main registration form which is form_2 has a filed for drop down and it is called Element_11 and it is a drop down and has a default value. This field is supposed to be the course description.

    If I select the drop down and a value that needs to be displayed should be the course Description which is what makes sense to the user. However, I like the courseID to be returned for saving in the database so an admin can see which course ID this person is trying to register. Am I driving you crazy with all of these? I hope not. But I can tell yo this much that you are for sure taking this form tool to the next level.

    I forgot to add that the drop down comes up with the right value pointing at the right form_3. It shows the course ID and displays the error as above at the end of the field.

    Thanks

    Posted 15 years ago #
  • mig2000
    Member

    Ok. I fixed it. It displays fine. I changed the value from element_1 to element_2 and it works fine. However, I still get the error as above.

    And the value that gets returned is ZERO in form_2 but the display works fine.

    Posted 15 years ago #
  • Josh
    Member

    Alex;

    Can I assume that you mean you get an error on the form page that says


    EOT; return ; } //Dropdown function display_select(Object){ //check for error = ''; = ''; * = ''; = ''; if(!empty()){ = 'class="error"'; = "

    "; } //check for required if(1){ * = "*"; } //check for guidelines if(!empty()){ = "

    "; } 123456 = ''; = false; foreach (Array as ){ if(){ = 'selected="selected"'; = true; }else{ = ''; } if(!empty(Array['element_'.11]['default_value'])){ = ''; if(Array['element_'.11]['default_value'] == ){ = 'selected="selected"'; } } 123456 .= " "; } if(!){ if(!empty(Array['element_'.11]['default_value'])){ 123456 = ''." ".123456; }else{ 123456 = ''." ".123456; } } = <<

    (btw; for this forum, wrap code in tags; it makes it more readable :) )

    If you get that error, then you have messed up the code somewhere - missed a semicolon, or a ?> perhaps?

    Posted 15 years ago #
  • Josh
    Member

    hmm... wrap code in "code" tags.
    where , = the less than sign (shift+,), and . = the greater than sign (shift+.), it should go like this:

    ,code. code goes here ,/code.
    very tricky to explain; wish it was written clearer below the reply box :)

    Posted 15 years ago #
  • mig2000
    Member

    Here is the link: http://www.mig2000.com/um/formdata/betaforms/view.php?id=2

    The drop down is showing the value ok. But the form_2 which s what you are looking at does not save the course name. It saves the value of zero.

    I am trying to look to where I have gone wrong.

    Thanks

    Posted 15 years ago #
  • Josh
    Member

    Ok, looking at that page; yes, you definitely broke the code somewhere in there: it's sending php code as html instead of processing it.
    If you know enough about the code to fix it, you could do that; or simply start over with a fresh copy of machform, and re-do the changes now that you know how :)

    In terms of the submission changing to zero; I never used the confirmation page; it's possible that it needs to be changed in a manner that is similar to post-functions.php ; try turning off the confirmation page, to see if it takes the proper input

    Posted 15 years ago #

  • RSS feed for this topic

    Reply »