www.webdeveloper.com
Results 1 to 3 of 3

Thread: auto populating a second dropdow from a 1st one

Hybrid View

  1. #1
    Join Date
    Apr 2012
    Posts
    14

    auto populating a second dropdow from a 1st one

    Hi everybody...

    I work on a site rigth now where I want to auto populate a second drop down menu from a 1st one. BUT I hit a wall and doesn't know where I do my fault.

    so to start here's my code.

    the front page is like this and working without the second drop down

    PHP Code:
    <?php
    include_once("db_connection.php");
    //////////////////////////////
    ?>
    <!doctype html public "-//w3c//dtd html 3.2//en">

    <html>

    <head>
    <title>THE 1/24-1/25 database</title>



    </head>

    <body >
    <div align="center"><?php




    @$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off


    if (isset($_POST['todo']) && $_POST['todo'] == "search") {    $todo=$_POST['todo'];

    $manufacturer_reel=$_POST['manufacturer_reel'];

    $query="select * from kit where  ";

    @
    $manufacturer_reel=$_GET['manufacturer_reel']; // Use this line or below line if register_global is off
    if(strlen($manufacturer_reel) > and !is_numeric($manufacturer_reel)){ // to check if $cat is numeric data or not. 
    echo "Data Error";
    exit;
    }


    $query=substr($query,0,(strLen($query)-4));

    echo 
    $query;
    echo 
    "<br><br>";
    $nt=mysql_query($query);
    echo 
    mysql_error();

    // End if form submitted



    }else{

    echo 
    "<form method=post action='search-keyword.php?go'><input type=hidden name=todo value=search>";
    ?>
    </div>
    <div align="center"></div>
    <div align="center"></div>
    <table width="960" border="0" align="center">
      
      <tr>
        <td width="355" align="center">&nbsp;</td>
        <td width="250">&nbsp;</td>
        <td width="355">&nbsp;</td>
      </tr>
      <tr>
        <td rowspan="2">&nbsp;</td>
        <td align="center"><?php
    //  strat of drop down  ///
    $q=mysql_query("SELECT DISTINCT manufacturer_reel FROM kit ORDER BY manufacturer_reel");
    echo 
    '<select name="manufacturer_reel">
    <option value="reel manufacturer">Please Choose an Option</option>'
    ;
    while(
    $row mysql_fetch_array($q)) {
    $thing $row['manufacturer_reel'];
    echo 
    '<option>'.$thing.'</option>';
    }
    echo 
    "</select>";

    //  end of 1st drop down  ///
    echo " <br> \n";
    echo 
    " <br> \n";


    ?>

      

      
      
      </td>
        <td><div align="left">Name Of vehicule manufacturer</div></td>
      </tr>
      
      <tr>
        <td align="center">&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      
      <tr>
        <td>&nbsp;</td>
        <td align="center"><?php 
        
    //  strat of drop down  ///
    $q=mysql_query("SELECT DISTINCT scale FROM kit ORDER BY scale");
    echo 
    '<select name="scale"><option value="scale">Please Choose an Option</option>';
    while(
    $row mysql_fetch_array($q)) {
    $thing $row['scale'];
    echo 
    '<option>'.$thing.'</option>';
    }
    echo 
    "</select>";

    echo 
    " <br> \n";
    echo 
    " <br> \n";

    ?></td>
        <td><div align="left">Scale of model</div></td>
      </tr>
      <tr>
        <td><strong>Clic the button at the bottom to perfom your search</strong></td>
        <td>&nbsp;</td>
        <td><div align="right"></div></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td align="center"><?php
    //  start of drop down  ///
    $q=mysql_query("SELECT DISTINCT manufacturer_kit FROM kit ORDER BY manufacturer_kit");
    echo 
    '<select name="manufacturer_kit"><option value="kit manufacturer">Please Choose an Option</option>';
    while(
    $row mysql_fetch_array($q)) {
    $thing $row['manufacturer_kit'];
    echo 
    '<option>'.$thing.'</option>';
    }
    echo 
    "</select>";


    //  end of drop down  ///
    echo " <br> \n";
    echo 
    " <br> \n";

    ?></td>
        <td><div align="left">Name Of kit manufacturer</div></td>
      </tr>
      <tr>
        <td rowspan="2">&nbsp;</td>
        <td>&nbsp;    </td> 
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td align="center"><?php
    //  strat of drop down  ///
    $q=mysql_query("SELECT DISTINCT year_prod_reel FROM kit ORDER BY year_prod_reel");
    echo 
    '<select name="year_prod_reel"><option value="vehicules year manufactured">Please Choose an Option</option>';
    while(
    $row mysql_fetch_array($q)) {
    $thing $row['year_prod_reel'];
    echo 
    '<option>'.$thing.'</option>';
    }
    echo 
    "</select>";
    //  end of drop down  ///
    echo " <br> \n";
    echo 
    " <br> \n";
     
     
    ?></td>
        <td><div align="left">
          <p>Year of production of the vehicule</p>
          <p>&nbsp;</p>
        </div></td>
      </tr>
      </table>
      
    <div align="center"></div>
    <table width="960" border="0">
                <tr>
                  <td>
                    <div align="center">
                      <?php
     
    echo "
    <br><input   name='name' action='search-keyword.php?go'

    input type='image' src='catalog/images/design_web/search-buton_en.jpg' width='272' height='54' border='0' alt='Submit' >
    </form>
    "
    ;
    }


    ?>
                    </div></td>
      </tr>
              </table>          
      </body>

    </html>
    I will continue in an other post... to be continue

  2. #2
    Join Date
    Apr 2012
    Posts
    14
    and this is the result page you see when you clic the submit button.

    PHP Code:
    <?php

    include_once("db_connection.php");
    //////////////////////////////
    ?>
    <!doctype html public "-//w3c//dtd html 3.2//en">

    <html>

    <head>
    <title>THE 1/24-1/25 database</title>

    </head>

    <body >
    <p align="center">&nbsp;</p>
    <table width="920" border="0">
      <tr>
        <td width="359"><p class="style5">You will find some TBC. TBC is for &quot;To Be Confirmed&quot;. If somebody know the missing information ( TBC ) it will be appreciate to send it to me at this mail: <a href="mailto:info@hobby-shop.qc.ca">info@hobby-shop.qc.ca</a> <br />
            <br />
    thanks in advance for your help</p>
        </td>
        <td width="161"><div align="center"><a href="search-db-foreach.php"><img src="catalog/images/design_web/othersearch.jpg"  border="0" alt="search" width="150" height="96"></a></div></td>
        <td width="386"> <div align="right">
          <p>This logo <img  src= 'http://www.scale24-25.com/catalog/images/design_web/PDF_logo.gif' alt="" width='50' height='50'/> mean that you can download a PDF file. To read it you need minimum Acrobat reader and you can download it <a href="http://get.adobe.com/reader/">here</a></p>
          <p>Some link will return a TBC. Sorry for any inconveniant. as soon as we will receive or do the PDF it will be updated.</p>
        </div></td>
      </tr>
    </table>


    <br>







    <?php
    if (isset($_POST['todo']) && $_POST['todo'] == "search") {    $todo=$_POST['todo'];

    $name=$_POST['manufacturer_reel'];
    $name1=$_POST['manufacturer_kit'];
    $name2=$_POST['year_prod_reel'];
    $name3=$_POST['scale'];


    //-query  the database table for the post field  

    $sql="SELECT  kit_id, kit_number, kit_name, description_eng, manufacturer_kit, manufacturer_reel, scale, engine_detail, year_prod_kit, year_prod_reel, image_id, image_id1, image_id2  FROM kit ";
        
    $sqlOperand="WHERE";
        if (
    $name != "reel manufacturer") {
            
    $sql $sql $sqlOperand " manufacturer_reel LIKE '%" $name ."%' ";
            
    $sqlOperand " AND ";
        }
        if (
    $name1 != "kit manufacturer") {
            
    $sql $sql $sqlOperand " manufacturer_kit LIKE '%" $name1 ."%' ";
            
    $sqlOperand " AND ";
        }
        if (
    $name2 != "vehicules year manufactured") {
            
    $sql $sql $sqlOperand " year_prod_reel LIKE '%" $name2 ."%' ";
            
    $sqlOperand " AND ";
        }
        if (
    $name3 != "scale") {
            
    $sql $sql $sqlOperand " scale LIKE '%" $name3 ."%' ";
            
    $sqlOperand " AND ";
        }
        if (
    $sqlOperand == "WHERE") {
               echo  
    "<p>Please enter a search query</p>";   
        } 
        
        else {
           
    //   echo '<img src="web-design/sorry.jpg" alt="sorry">';
               
           // echo "Sorry nothing match your selection / Desolle rien ne correspond a votre selection";
           // echo "run sql: <BR> $sql <BR> and display results";
        
    }


     
      
    //-run  the query against the mysql query function   
       
    $result=mysql_query($sql);  
       
      
    //-make the header of the table   
       
       
    echo "  <table align=center width=\"1340\" border=\"\">\n";
       echo 
    "    <tr>\n";
       echo
    "  <td nowrap align=center width='95'>  kit manufacturer  \n";        
       echo
    "  <td nowrap align=center width='55'>    scale \n";        
       echo
    "  <td nowrap align=center width='90'>    kit number \n";        
       echo
    "  <td nowrap align=center width='290'>    kit name \n";        
       echo
    "  <td nowrap align=center width='400'>    description \n";
       echo
    "  <td nowrap align=center width='125'>    vehicule year \n";        
       echo
    "  <td nowrap align=center width='80'>    complete engine detail\n";        
       echo
    "  <td nowrap align=center width='80'>    Select Picture to get bigger\n";
       echo
    "  <td nowrap align=center width='75'>    instruction sheet\n";
       echo
    "  <td nowrap align=center width='75'>    box contain\n";
       echo 
    "    <tr>\n";

       
    //-create  while loop and loop through result set   
       
    while($row=mysql_fetch_array($result)){   
             
    $description_eng=$row['description_eng'];   
             
    $manufacturer_kit=$row['manufacturer_kit']; 
             
    $scale=$row['scale']; 
             
    $manufacturer_reel=$row['manufacturer_reel'];   
             
    $kit_id=$row['kit_id']; 
             
    $kit_number=$row['kit_number'];
             
    $kit_name=$row['kit_name'];
             
    $engine_detail=$row['engine_detail'];
             
    $year_prod_reel=$row['year_prod_reel'];
             
    $image_id=$row['image_id'];
             
    $image_id1=$row['image_id1'];
             
    $image_id2=$row['image_id2'];

             
               
       
    //-create  table of item during he while loop
       
    echo "  <table align=center width=\"1340\" border=\"\">\n";           
    echo
    "  <td nowrap align=center width='95'>  $manufacturer_kit  \n";    
    echo
    "  <td nowrap align=center width='55'>  $scale  \n";
    echo
    "  <td nowrap align=center width='90'>    $kit_number \n";        
    echo
    "  <td nowrap align=center width='290'>    $kit_name \n";        
    echo
    "  <td nowrap width='400'>    $description_eng \n";
    echo
    "  <td nowrap align=center width='125'>    $year_prod_reel \n";        
    echo
    "  <td nowrap align=center width='80'>    $engine_detail \n";    
    if(!empty(
    $image_id))    echo"  <td nowrap width='80'> <a href= ".$image_id." target='_blank'> <img src= '".$image_id."' width='75' height='50' border='0'/>";else    echo "  <td nowrap width='75'>    <img src='http://www.scale24-25.com/catalog/images/design_web/tbc.jpg' width='50' height='50' border='0'/>";
    if(!empty(
    $image_id1))    echo"  <td nowrap width='75'> <a href= ".$image_id1." target='_blank'> <img src='http://www.scale24-25.com/catalog/images/design_web/PDF_logo.gif' width='50' height='50' border='0'/>";else    echo "  <td nowrap width='75'>    <img src='http://www.scale24-25.com/catalog/images/design_web/tbc.jpg' width='50' height='50' border='0'/>";
    if(!empty(
    $image_id2))    echo"  <td nowrap width='75'> <a href= ".$image_id2." target='_blank'> <img src='http://www.scale24-25.com/catalog/images/design_web/PDF_logo.gif' width='50' height='50' border='0'/>";else    echo "  <td nowrap width='75'>    <img src='http://www.scale24-25.com/catalog/images/design_web/tbc.jpg' width='50' height='50' border='0'/>";

    echo 
    "    </tr>\n";
    echo 
    "    </table>\n"
    echo 
    "    </tr>\n";
    echo 
    "    </table>\n";
     

       }   
       }  
       else{   
       echo  
    "<p>Please enter a search query</p>";   
       }   
       
     
    ?>  


      



    </body>

    </html>

    now from these file I want to add a second drop down undert the Name Of vehicule manufacturer. this drop down will auto populate with the model found in the database attached to the manufacturer EX: you select chevy and it will found every model of chevy and display them like impala, caprice etc..

    I found on the web this... http://roshanbh.com.np/2007/12/chang...x-and-php.html

    it's exactly what I want to do so I try it by changing his value by mine but ... it doesn't work.

    here is what I've done.

    first I create the model database where I put all the model I have

    after i create search_model.php like they do (city.php)

    her's the code
    PHP Code:
    <?php
    include_once("db_connection.php");
    //////////////////////////////
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php 
    $query
    ="select distinct model from model where modelid=$model";
    $result=mysql_query($query);?>
    <select name="model">
    <option>Select Model</option>
    <? while($row=mysql_fetch_array($result)) { ?>
       <option value><?=$row['model']?></option>
    <? ?>
    </select>
    </body>
    </html>
    and than I modify my first page and this is where I stuck for now. I got an error message saying


    so my modify code are like this ( I put only the section I modify )

    PHP Code:
    //  strat of drop down  ///
    $q=mysql_query("SELECT DISTINCT manufacturer_reel FROM kit ORDER BY manufacturer_reel");
    echo 
    '<select name="manufacturer_reel"  nChange="getModel('search_model.php?model='+this.value)">
    <option value="reel manufacturer">Please Choose an Option</option>'
    ;
    while(
    $row mysql_fetch_array($q)) {
    $thing $row['manufacturer_reel'];
    echo 
    '<option>'.$thing.'</option>';
    }
    echo 
    "</select>";

    //  end of 1st drop down  ///
    echo " <br> \n";
    echo 
    " <br> \n";
    //  start of 2nd drop down  ///

    ?>
    <div id="modeldiv"><br /><br />
      <select name="select">
        <option>Select Model</option>
      </select> 
    this is the error message I got

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in ....search-db-foreach.php on line 120

    the the line 120 is this one

    PHP Code:
    echo '<select name="manufacturer_reel"  nChange="getModel('search_model.php?model='+this.value)"> 
    so what's I've done wrong ?

    thanks in advance

    S

  3. #3
    Join Date
    Apr 2012
    Posts
    14
    I forgot to tell you thet I've made the function too and this is the code

    PHP Code:
    <SCRIPT language=JavaScript>
    function 
    getModel(strURL)
    {         
     var 
    req getXMLHTTP(); // fuction to get xmlhttp object
     
    if (req)
     {
      
    req.onreadystatechange = function()
     {
      if (
    req.readyState == 4) { //data is retrieved from server
       
    if (req.status == 200) { // which reprents ok status                    
         
    document.getElementById('modeldiv').innerHTML=req.responseText;
      }
      else
      { 
         
    alert("There was a problem while using XMLHTTP:\n");
      }
      }            
      }        
    req.open("GET"strURLtrue); //open url using get method
    req.send(null);
     }
    }
    </script> 
    hope this help

    yours
    S

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



Recent Articles