SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with generating dynamic dropdowns?

    I think that's what you call it.

    Code:
    $dir = 'gallery/';
    $categories = scandir($dir);
    $categories = array_slice($categories, 2);
    
    
    
    
    
    
    
    <p><select name=\"category\">\n<option>Choose a Category:</option>\n";
    foreach ($categories as $value) {
    
    
    if ($_GET['class'] == $value) {
        $content .= "<option selected value=\"$value\">$value</option>\n";
    } else {
        $content .= "<option value=\"$value\">$value</option>\n";
    }
    
    
    
    }

    So I'm wanting to add an additional dropdown. Right now it's looking in the gallery/ directory and grabbing all folders for the dropdown. Once you choose one, I want to grab all folders inside THAT folder, and do the same. So depending on what option you choose, it would generate another list of directories. Just not sure how to do this.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yes, they are indeed called dynamic dropdown lists.
    To do what you want you'd need AJAX (Asynchronous Javascript and XML).

    Here is an article with an example for countries, but you can customize it to whatever you want
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2008
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about with just straight javascript? I found a script that works, but I'm not familiar enough with javascript to do one final step on it.


    Here is the script part that tells it what options to choose. I believe I can put bits of php in here to generate my options, would that be correct to assume that? Or will it mess the javascript up to put in php for generating?

    Code:
    <script language="javascript" type="text/javascript">
    function dropdownlist(listindex)
    {
    
    document.formname.subcategory.options.length = 0;
    switch (listindex)
    {
    
    case "Alabama International Dragway 5-21-10 - 5-23-1" :
    document.formname.subcategory.options[0]=new Option("Select Sub-Category","");
    document.formname.subcategory.options[1]=new Option("Air-Conditioners/Coolers","Air-Conditioners/Coolers");
    document.formname.subcategory.options[2]=new Option("Audio/Video","Audio/Video");
    document.formname.subcategory.options[3]=new Option("Beddings","Beddings");
    document.formname.subcategory.options[4]=new Option("Camera","Camera");
    document.formname.subcategory.options[5]=new Option("Cell Phones","Cell Phones");
    
    break;
    
    case "Atlanta Dragway 4-16-10 - 4-18-10" :
    document.formname.subcategory.options[0]=new Option("Select Sub-Category","");
    document.formname.subcategory.options[1]=new Option("Colleges","Colleges");
    document.formname.subcategory.options[2]=new Option("Institutes","Institutes");
    document.formname.subcategory.options[3]=new Option("Schools","Schools");
    document.formname.subcategory.options[4]=new Option("Tuitions","Tuitions");
    document.formname.subcategory.options[5]=new Option("Universities","Universities");
    
    break;
    
    
    }
    return true;
    }
    </script>
    Then I have this now for my dropdowns:

    Code:
    $dir = 'gallery/';
    $categories = scandir($dir);
    $categories = array_slice($categories, 2);
    
    
    
    
    
    
    if (!isset($_GET['subpage'])) {   // Image Upload Form Below
      $content .= "<form method=\"post\" id=\"formname\" name=\"formname\" action=\"index.php?page=gallery&add=new&subpage=upload\" enctype=\"multipart/form-data\">
    
    
    
    <p><select name=\"category\" onchange=\"javascript: dropdownlist(this.options[this.selectedIndex].value);\">\n<option>Choose a Category:</option>\n";
    
    foreach ($categories as $value) {
    
    
    if ($_GET['class'] == $value) {
        $content .= "<option selected value=\"$value\">$value</option>\n";
    } else {
        $content .= "<option value=\"$value\">$value</option>\n";
    }
    
    
    
    }
    
    $content .= "</select>\n</p>
    
    <p>
    <script type=\"text/javascript\" language=\"JavaScript\">
    document.write('<select name=\"subcategory\"><option value=\"\">Select Sub-Category</option></select>')
    </script>
    <noscript>
    
    <select name=\"subcategory\" id=\"subcategory\" >
    <option value=\"\">Select Sub-Category</option>
    </select>
    </noscript>
    
    </p>
    
    File:<br />
      <input type=\"file\" name=\"imagefile\" class=\"form\">
      <br /><br />
      <input name=\"submit\" type=\"submit\" value=\"Sumbit\" class=\"form\">  <input type=\"reset\" value=\"Clear\" class=\"form\">
      </form>";
    So I have two dropdowns, second one is generating based on the choice. This is working correctly. Will I be able to, with the javascript, keep the options chosen, selected after submitting the form? It's an image upload script, after uploading one image, the same page reloads and I need the dropdown menus to be the same choices as I just chose. I could do this with one and before added the javascript, pretty easy with this part:

    Code:
    if ($_GET['class'] == $value) {
        $content .= "<option selected value=\"$value\">$value</option>\n";
    } else {
        $content .= "<option value=\"$value\">$value</option>\n";
    }
    Would I be able to do something as simple as that in my top javascript bit when I do the php part?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by aebstract View Post
    Here is the script part that tells it what options to choose. I believe I can put bits of php in here to generate my options, would that be correct to assume that? Or will it mess the javascript up to put in php for generating?
    If it's in a PHP file, then yes, you can use PHP to generate javascript. Won't mess up the javascript.
    I'm assuming you know what the script does and how to change it to your requirements?

    Quote Originally Posted by aebstract View Post
    Will I be able to, with the javascript, keep the options chosen, selected after submitting the form? It's an image upload script, after uploading one image, the same page reloads and I need the dropdown menus to be the same choices as I just chose. I could do this with one and before added the javascript, pretty easy with this part:

    Would I be able to do something as simple as that in my top javascript bit when I do the php part?
    Yes, but it's not as easy as with PHP (although the concept is the same). Google for "javascript query string".

    I'd suggest you get the dropdowns working first and only then worry about selecting the proper one on page reload, otherwise you might get massively confused
    (I know I always get confused when I want to get all parts of a program to work at the same time, it's way better to divide and conquer).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •