Page 1 of 6 123 ... LastLast
Results 1 to 10 of 53

Thread: Help with Dropdown Menu Javascript

  1. #1
    Join Date
    Jun 2009
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with Dropdown Menu Javascript

    Hi,

    I am trying to create a PHP/Javascript dropdown menu that gets its results from a My SQL Table. Basiclly its three dropdown menus, the user selects their reigion in the first one, this then searches the My SQL table and finds the reigons, after the user has selected their reigon the second drop down box displays the Gardens/Shows based upon the first results, the third then shows the dates of that event based upon what Garden/Shows the user picks.

    The problem that I am having is the fact that it does not seem to display the data from the table, all of the PHP/ My SQL is fine.

    The code for it is as follows:

    HTML Code:
    <script language="javascript" src="update.php"></script>
    <form name="drop_list" action="sucess.php" method="post"><select name="Main" onchange="SelectCat();">
    <option value="">Reigon</option></select>&nbsp;<select id="Category" name="Category" onchange="SelectSubCat();">
    <option value="">Garden/Show</option></select><select id="SubCat" name="SubCat">
    <option value="">When/Where</option></select></form>
    The code for the update.php:

    PHP Code:
    <?php

    require "Connection.php";
    echo 
    "

    function fillCategory(){ 

    "
    ;
    $q1=mysql_query("select * from `group`");
    echo 
    mysql_error();
    while(
    $nt1=mysql_fetch_array($q1)){
    echo 
    "addOption(document.drop_list.Main, '$nt1[group]', '$nt1[group]');";
    }
    ?>
    }

    function SelectCat(){

    removeAllOptions(document.drop_list.Category);
    addOption(document.drop_list.Category, "", "Category", "");

    <?

    $q2
    =mysql_query("select distinct(`group`) from category");

    while(
    $nt2=mysql_fetch_array($q2)){

    echo 
    "if(document.drop_list.Main.value == '$nt2[group]'){";
    $q3=mysql_query("select category from category where `group`='$nt2[group]'");
    while(
    $nt3=mysql_fetch_array($q3)){
    echo 
    "addOption(document.drop_list.Category,'$nt3[category]', '$nt3[category]');";

    }
    echo 
    "}";
    }
    ?>
    }
    The code for sucess.php:

    PHP Code:
    function removeAllOptions(selectbox)
    {
    var 
    i;
    for(
    i=selectbox.options.length-1;i>=0;i--)
    {
    selectbox.remove(i);
    }
    }

    function 
    addOption(selectboxvaluetext )
    {
    var 
    optn document.createElement("OPTION");
    optn.text text;
    optn.value value;

    selectbox.options.add(optn);
    }

    Hope you can help, Thanks

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    I'm not able to write any code right now, but I suggest that you try to debug the JavaScript from wthin a static copy of the page rather than developing PHP and JS together.

    However, you might want to try placing this at the PHP's start, just to make sure...
    Code:
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', 1);
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. #3
    Join Date
    Jun 2009
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Thank-You for replying Jesdisciple,

    I have tryed your suggestion about the error reporting and it does not report any errors.

    I am not desperate for the code yet, so if you could it would be greatfully appreciated.

    Thanks again

  4. #4
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    What does update.php look like after the PHP is run? I really don't think it's a good idea to echo an error into JS.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  5. #5
    Join Date
    Jun 2009
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    The Update.php looks like this:

    PHP Code:
    function fillCategory(){ addOption(document.drop_list.Main'Scotland''Scotland');addOption(document.drop_list.Main'North East''North East');addOption(document.drop_list.Main'North West''North West');addOption(document.drop_list.Main'Yorkshire''Yorkshire');addOption(document.drop_list.Main'Wales''Wales');addOption(document.drop_list.Main'West Midlands''West Midlands');addOption(document.drop_list.Main'East Midlands''East Midlands');addOption(document.drop_list.Main'South West''South West');addOption(document.drop_list.Main'South East''South East');} function SelectCat(){ removeAllOptions(document.drop_list.Category); addOption(document.drop_list.Category"""Category"""); if(document.drop_list.Main.value == 'Scotland'){addOption(document.drop_list.Category,'Gardening Scotland Show''Gardening Scotland Show');addOption(document.drop_list.Category,'Royal Botanic Gardens Edinburugh''Royal Botanic Gardens Edinburugh');}if(document.drop_list.Main.value == 'North East'){addOption(document.drop_list.Category,'Crook Hall Gardens''Crook Hall Gardens');addOption(document.drop_list.Category,'Lancoshire Show''Lancoshire Show');}if(document.drop_list.Main.value == 'North West'){addOption(document.drop_list.Category,'RHS Show Tatton Park''RHS Show Tatton Park');addOption(document.drop_list.Category,'Knowsley Flower Show''Knowsley Flower Show');addOption(document.drop_list.Category,'Fletcher Moss Botanical Gardens''Fletcher Moss Botanical Gardens');}if(document.drop_list.Main.value == 'Yorkshire'){addOption(document.drop_list.Category,'RHS Harlow Carr''RHS Harlow Carr');addOption(document.drop_list.Category,'Harrogate Autumn Flower Show''Harrogate Autumn Flower Show');}if(document.drop_list.Main.value == 'Wales'){addOption(document.drop_list.Category,'RHS Spring Show Cardiff''RHS Spring Show Cardiff');addOption(document.drop_list.Category,'National Botanic Gardens of Wales''National Botanic Gardens of Wales');}if(document.drop_list.Main.value == 'West Midlands'){addOption(document.drop_list.Category,'BBC Gardeners World Live''BBC Gardeners World Live');addOption(document.drop_list.Category,'Charlecote Park''Charlecote Park');addOption(document.drop_list.Category,'Birmingham Botanical Gardens''Birmingham Botanical Gardens');}if(document.drop_list.Main.value == 'East Midlands'){addOption(document.drop_list.Category,'Linconshire Show''Linconshire Show');addOption(document.drop_list.Category,'Renishaw Hall''Renishaw Hall');addOption(document.drop_list.Category,'Chatsworth Derbyshire''Chatsworth Derbyshire');}if(document.drop_list.Main.value == 'South West'){addOption(document.drop_list.Category,'RHS Garden Rosemoor''RHS Garden Rosemoor');addOption(document.drop_list.Category,'Killerton House and Gardens''Killerton House and Gardens');addOption(document.drop_list.Category,'Eden Project Cornwall''Eden Project Cornwall');}if(document.drop_list.Main.value == 'South East'){addOption(document.drop_list.Category,'RHS London Harvest Show''RHS London Harvest Show');addOption(document.drop_list.Category,'Royal Botanic Gardens Kew''Royal Botanic Gardens Kew');addOption(document.drop_list.Category,'RHS Hampton Court Palace Flower Show''RHS Hampton Court Palace Flower Show');}} 

  6. #6
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    =\ I use PHP to generate dynamic JS (and CSS) myself, but PHP shouldn't get in the way of debugging the JavaScript. In fact, I'm curious whether you're actually gaining anything by doing that, as no variables seem to be passed to the PHP to influence its output.

    So, in the interest of looking at everything after PHP, can you give a URL which demonstrates the problem? (This is actually standard practice on DD, but I guess I'm still learning why.) I won't be looking at anything until I can avoid the PHP.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  7. #7
    Join Date
    Jun 2009
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Dear Jesciple,

    Here is the URL of the drop down menu: http://amateur-gardeners.co.uk/Gardens-and-Shows.php

    As you can see nothing is shown in the drop down menus.

    Thanks again

  8. #8
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    There's a syntax error on the very last line of this script:
    Code:
    syntax error
    http://amateur-gardeners.co.uk/sucess.php
    Line 22
    The line reads (\n is a real newline in the file):
    Code:
    [\n
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  9. #9
    Join Date
    Jun 2009
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    How would I fix this error?

    Thanks

  10. #10
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Just remove the line... It's invalid because you start an array and never finish it.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

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
  •