Jump to content

question about how to populating a SUB drop down from an other one with PHP code


Recommended Posts

Hi!! the question look simple at his base because I will receive TONS of answer saying ... " hey men use javascript OR ajax "  but I don't want to use these... I want to populate the second dropdown menu dynamicly with PHP code. and sql query.

 

why?? because I have over 43000 possibility and doing a script manually is impossible. the database is changing every hour.

 

this is the code of my working page.

 

the form

 

NOTE: i REMOVE ALL DB CONNECTION FOR SECURITY PURPOSE. THANKS FOR YOUR UNDERTANDING.

 

 

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>(Type a title for your page here)</title>
</head>

<body >
<img src="http://www.scale24-25.com/images/banners/banner.jpg" alt="banner" width="997" height="213" border="0" usemap="#Map" />
<map name="Map" id="Map">
<area shape="poly" coords="0,100,85,102,92,55,108,50,326,51,323,7,3,3" href="http://www.hobby-shop.qc.ca" alt="hobby-shop" />
<area shape="poly" coords="672,57,891,53,887,4,995,4,985,108,673,102" href="http://www.hobby-shop.qc.ca" alt="hobby-shop" />
<area shape="poly" coords="326,13,330,53,98,54,90,92,670,99,668,54,889,51,886,5" href="http://www.scale-auto-style.com" alt="" />
</map>
<?php

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

$manufacturer_reel=$_POST['manufacturer_reel'];

$query="select * from kit where  ";

////////// Including manufacturer_reel field search ////
if(strlen($manufacturer_reel) > 0 ){
$query.= " manufacturer_reel='$manufacturer_reel' and "; 
}  
//// End of class field search ///////////

$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>";
?>
<table width="960" border="0">
  <tr>
    <td colspan="3"><p align="center"></td>
  </tr>
  <tr>
    <td><div align="right"></div></td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td><div align="right">Echelle du modele</div></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> </td>
    <td> </td>
    <td><div align="right"></div></td>
  </tr>
  <tr>
    <td><div align="right">Nom du manufacturier automobile</div></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 drop down  ///
echo " <br> \n";
echo " <br> \n";
//  end of drop down  ///

?></td>
    <td><div align="left">Name Of vehicule manufacturer</div></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td><div align="right"></div></td>
  </tr>
  <tr>
    <td><div align="right">Nom du manufacturier de modele reduit</div></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><div align="right"></div></td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td><div align="right">Annee de fabrication du Vehicule</div></td>
    <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">Year of production of the vehicule</div></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td align="center"> </td>
    <td> </td>
  </tr>
  <tr>
    <td><div align="right"><strong></strong></div></td>
    <td align="center">
<?php

echo "
<br><input type=submit value=Search name='name' action='search-keyword.php?go' >
</form>
";
}


?>
<td><div align="left"><strong></strong></div></td>






</body>

</html>

 

AND THE PAGE RESULT

 

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>(Type a title for your page here)</title>

</head>

<body >
<p align="center"><img src="http://www.scale24-25.com/catalog/images/banners/banner.jpg" alt="banner" width="997" height="213" border="0" usemap="#Map" />
<map name="Map" id="Map">
<area shape="poly" coords="0,100,85,102,92,55,108,50,326,51,323,7,3,3" href="http://www.hobby-shop.qc.ca" alt="hobby-shop" />
<area shape="poly" coords="672,57,891,53,887,4,995,4,985,108,673,102" href="http://www.hobby-shop.qc.ca" alt="hobby-shop" />
<area shape="poly" coords="325,11,329,51,97,52,89,90,669,97,667,52,888,49,885,3" href="http://www.scale-auto-style.com" alt="" />
</map></p>
<table width="920" border="0">
  <tr>
    <td width="359"><p class="style5">You will find some TBC. TBC is for "To Be Confirmed". 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"> </td>
    <td width="386"> <div align="right">
      <p>This logo <img  src= 'http://www.scale24-25.com/images/PDF_logo.gif' 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, 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  =$row['description'];   
         $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 \n";
echo"  <td nowrap align=center width='125'>	$year_prod_reel \n";		
echo"  <td nowrap align=center width='80'>	$engine_detail \n";	
echo"  <td nowrap width='80'> <a href=".$image_id.">   <img src='".$image_id."' width='75' height='50' border='0'/>";
echo"  <td nowrap width='75'> <a href= ".$image_id1.">   <img src= 'http://www.scale24-25.com/images/PDF_logo.gif' width='50' height='50' border='0'/>";
echo"  <td nowrap width='75'> <a href=".$image_id2.">   <img src='http://www.scale24-25.com/images/PDF_logo.gif' 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>

 

YOU CAN SEE IN THE FORM i HAVE A DROP DOWN MENU CALL Name Of vehicule manufacturer .. IN VACT THIS IS THE REAL CAR MANUFACTURER EX: TOYOTA, gmc ETC..  WHEN THE USER WILL SELECT ONE OF THESE AN OTHER DROPDOWN ( NOT SHOWN ) WILL DISPLAY EVERY MODEL FOUND IN THE DATABASE IN RELATION WITH THE MANUFACTURE. IN FACT IT'S LOCATED IN THE TABLE.

 

SO HOW I CAN GET THIS RELATION??

 

YOURS

 

SEBASTIEN

if you want to populate the second dropdown dynamically using PHP, you have 2 practical choices: 1. Ajax (Javacript calling PHP on the server and updating the dropdown without a page refresh), or 2. submit the form on the onchange() event of the first dropdown and have PHP rebuild the page with the updated content.

 

Option 1 and 2 have the same practical result (dealing with bots is the sand in the vaseline), but option 1 doesn't require a page refresh.

Just to second what BlueSkyIS stated, you state you want to update the second select list dynamically. You should know that once a page is delivered to the browser PHP has no way of interacting with the page by itself. You either need to utilize JavaScript (which can initiate a PHP call) or you can have the user make a selection from the primary drop-down and manually submit the page. AJAX IS the solution you are looking for - you just don't know it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.