Results 1 to 9 of 9

Thread: [RESOLVED] dynamic select php

  1. #1
    Join Date
    Jul 2007

    resolved [RESOLVED] dynamic select php

    I need to make a dynamic dropdown ( select box) that grabs some categories I am storing in the database. The problem is once the user selects the category I need to run a query that selects all the sub categories underneath that category and keeps on populating till there is no more sub-sub categories to show up.

    If anyone can point me in the right direction.

  2. #2
    Join Date
    Apr 2007
    If you're good with javascript then ajax is the way to go. If you're OK with PHP but not so good with javascript, xajax is the way to go.

    http://xajaxproject.org has a 10 minute tutorial that should get you started. It's a lot easier that you might first think.

    The first drop down would have an onchange event which passes the value to an xajax function. The xajax function looks up the info for the next select and displays it.

    You can achieve all this in javascript too but you'd need to send all the info to the browser up front. Ajax allow just the required info to be sent.

    Rather than use selects it would be a nicer interface if you used a collapsing folder tree structure.

    Here's a jquery tree with PHP integration

    Theres others out there too.

  3. #3
    Join Date
    Jul 2007


    Thank you!!!! I won't be able to use the collapsing folder structure for this project even though that looks awesome, But I am sure I will get to use soon in something else. I have been good enough to use just the basic javascript so far but more and more programming I do, more projects with ajax keep coming up.

  4. #4
    Join Date
    Apr 2007
    You're welcome! The only tricky part of using xajax is setting the page up to include trhe files after that it's really quite easy and handles all the tough javascript stuff, all you really need is javascript events like onclick, onload etc. There's a forum at http://xajaxproject.org which appears pretty slow but there are 3 or 4 core users (q-no, BBC, Ed, Jared) that are in there all the time and really know their stuff. I can help up to a point with xajax but even though I've been using it for over a year I only need it to call my PHP functions ajaxically (hey a new word!). But it has been great and easy.

  5. #5
    Join Date
    Aug 2004
    70 klicks above the Do Lung bridge
    I would definately say use ajax. I had to do something similar for my degree and used ajax for this. I would recommend breaking down w3schools' suggest example and working tweaking it for your use. If you're still stuck PM me and I'll send you my source code.

  6. #6
    Join Date
    Jul 2007


    I tried axjax but I was using zencart and can't have spaces in the axjax in the header and if I would have taken out all the space would have taken me forever in the zencart files. I am open to any suggestion to make this better, but its just a modified version from w3schools.

    IN your main file put call the javscript showUser function on change in the select box
    then save the following code as test.php and change that in the javascript file.
    // make a connection to the database here
    //$sql="select c.categories_id, c.parent_id, p.categories_name from categories c JOIN
    // categories_description p ON (c.categories_id = p.categories_id) WHERE c.parent_id =
    // any(select c.categories_id from categories_description c where c.categories_name = ".(int)$_GET['cid'] . ")";
    $sql="select c.categories_id, c.parent_id, p.categories_name from categories c
    INNER JOIN categories_description p ON (c.categories_id = p.categories_id)
    WHERE c.parent_id = ".(int)$_GET['cid'];

    $result = mysql_query($sql);
    if ($result)
    echo "<div id = 'dropdownMenu{$q}sub'>";
    echo "<select name='sub-$q' onchange='showUser(this.value,$q)'><option value=''>Please Select Category</option>";
    while($row = mysql_fetch_array($result))

    echo "<option value={$row['categories_id']}>". $row['categories_name'] . "</option>";

    } else {
    echo mysql_error($con);
    echo "</select>";

    echo '<div id ="dropdownCat'.($q+1).'"sub">';
    <b>Customer info will be listed here.</b>

    var xmlHttp

    var id_cnt = 0;

    function showUser(cat_id,id)
    if (xmlHttp==null)
    alert ("Browser does not support HTTP Request")
    var url="test.php"
    id_cnt = id+1;
    url=url+"?q="+(id+1) + "&cid=" + cat_id

    function stateChanged(id)

    // console.log(xmlHttp);
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    document.getElementById("dropdownCat" + id_cnt).innerHTML = xmlHttp.responseText;


    function GetXmlHttpObject()
    var xmlHttp=null;
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    catch (e)
    //Internet Explorer
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    catch (e)
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    return xmlHttp;

  7. #7
    Join Date
    Mar 2007
    Why not write the page so that is submits to itself when the on change happens and have the 2nd query run and populate a second drop down field and then display the page again with all its current selections, this would require some scripting to do this but it means that your only using PHP and a database and not relying on several technologies on server and client side.

    I can see where the guys are coming from when they say use AJAX but this can have some working problems as you seem to be finding out.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  8. #8
    Join Date
    Jul 2007
    Thanks for suggestion. I did think about it, but I wanted to use ajax(learn something new) and an assumption that going with that approach will take longer to run on the page instead of using AJAX(as its reloading the page again once someone clicks on the drop down, I may be wrong with that though but thats what I have noticed)

    Also, it may be misleading in my previous post. I was able to fix this issue with ajax, Before that I tried using axjax but, after I installed axjax and fixed all the bugs I noticed I couldn't output a space or anything before I called axjax on the page itself. Now thats a no-go since it was a zen cart project and that uses multiple files before even loading the page itself.
    So finally tried ajax and used this script. Thanks to all guys who helped me at work and at webdeveloper to learn something new. I am surprised there is not a forum for ajax itself though at WD.

  9. #9
    Join Date
    Aug 2004
    70 klicks above the Do Lung bridge
    Generally the Javascript forum is where you should post your AJAX questions, although, if you have an issue, it could easilly be PHP or XML that is the cause of the problem.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.13208 seconds
  • Memory Usage 2,925KB
  • Queries Executed 13 (?)
More Information
Template Usage (32):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (9)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates