Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Dynamically populating one dropdown menu, from value selected in another dropdown

    Hi, i have a database filled with things and the way i want the client to be able to edit it is by first selecting the product from one drop down menu, then from whatever they choose in that drop down menu, the second is populated with the information that goes with it. Heres what i have:
    PHP Code:
    <?php 
            
    if($session->isAdmin()){
        
    ?>
            <form action="" method="post" name="gun" class="forms" enctype="multipart/form-data">
            Select a Manufacturer:<br />
            <select name="manufacturer">
            <option value=""></option>
        <?php 
            $query 
    "SELECT * FROM `gun_manufacturers`";
            
    $result mysql_query($query$conn);
            
    $numrows mysql_num_rows($result);
            for (
    $i=0$i $numrows$i++)
            {
                
    $row mysql_fetch_array($resultMYSQL_ASSOC);
                echo 
    "<option value=\"{$row['gunManufacturerID']}\">{$row['gunManufacturerName']}</option>";
            }
            
    ?>
            </select><br />
            <select name="gun">
            <?php
            $gunManufacturerID 
    = ( get_magic_quotes_gpc() ) ? $_POST['manufacturer'] : addslashes$_POST['manufacturer'] );
            
    $query "SELECT * FROM `guns` WHERE `gunManufacturerID = {$gunManufacturerID}";
            
    $result mysql_query($query$conn);
            
    $numrows mysql_num_rows($result);
            for (
    $i=0$i $numrows$i++)
            {
                
    $row mysql_fetch_array($resultMYSQL_ASSOC);
                echo 
    "<option value=\"{$row['gunID']}\">{$row['gunName']}</option>";
            }
            
            
    ?>
            </select>
            </form>
            <?
            
    } else {
                echo 
    "Sorry you do not have access to this page.";
            }
            
    ?>
    I know there is probably an easier way to do those queries too with joins, but im not too experienced with them and I can't figure out what the join would look like. Any help is greatly appreaciated

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    anyone?

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    i think you should use jumpmenus and their targets would be to refresh the same page but pass variables through so that the next jumpmenu and do a $_GET then a query based on that $_GET then build the menu

  • #4
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    any idea what that code would look like?

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    well this is how dreamweaver does jump menus
    PHP Code:
    <?
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function 
    MM_jumpMenu(targ,selObj,restore){ //v3.0
      
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
      if (
    restoreselObj.selectedIndex=0;
    }
    //-->
    </script>
    </head>

    <body>
    <form name="form1" method="post" action="">
      <select name="menu1" onChange="MM_jumpMenu('parent',this,0)">
        <option value="test.php?Q=SELECT * FROM table BLAH" selected>test</option>
      </select>
    </form>
    ?>
    now you could set the value = your page.php?then the query then for the next menu you could do

    $query = $_GET['Q'];
    $result = mysql_query($query) or die ("COULD NOT GET QUERY FROM URL: ".mysql_error());
    echo "<select name =\"xxx\">"
    while($row = mysql_fetch_array($result)){
    then youll bulid the menu here
    echo "<option value =\"xxx\">".$row['table column name']."</option>"
    }
    echo "</select>";

    or you could just keep building jump menu's until you reach you desired limit

  • #6
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    oh nice man let me see if this works, thanks.

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Posts
    111
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this keeps returning, "Query was empty".
    PHP Code:
    <script language="javascript" type="text/javascript">
        <!--
        function MM_jumpMenu(targ,selObj,restore){ //v3.0
          eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
          if (restore) selObj.selectedIndex=0;
        }
        //-->
        </script>
            <form action="" method="post" name="gun" class="forms" enctype="multipart/form-data">
            Select a Manufacturer:<br />
            <select name="manufacturer" onChange="MM_jumpMenu('parent',this,0)"> 
            <option value="edit_guns.php?Q=SELECT * FROM guns LEFT JOIN gun_manufacturers ON guns.gunManufacturerID=gun_manufacturers.gunManufacturerID" selected>Select a Manufacturer</option>
            </select>
            </form>
            <?
            $query 
    $_GET['Q'];
            
    $result mysql_query($query$conn) or die (mysql_error());
            echo 
    "<select name =\"gun\">";
            while(
    $row mysql_fetch_array($result)) {
            echo 
    "<option value =\"{$row['gunID']}\">".$row['gunName']."</option>";
            }
            echo 
    "</select>";    
            } else {
                echo 
    "Sorry you do not have access to this page.";
            }
            
    ?>

  • #8
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    i don think you need php for this because i seen a site earlier that let you do it without refreshing the page, but didnt save the link sorry.

    you have you initial jump menu that has to be poplulated
    <option value = xxx.php?q=blah>
    and that menu's action will refresh the page with variables being passed in the url

    the scond menu will pull those variables. you have the first menu doing it


  •  

    Posting Permissions

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