Jump to content

*solved* Dynamic Dropdowns using PHP - Tutorial/code ??


Recommended Posts

Hi

i remember seeing a tutorial or a code - cant remember which on creating a dynamic dropdowns using php (not js or ajax ).

I need something similar to selecting 1 country from a list of countries first and then the cities of the chosen country second and both choices being part of the same form.

If anyone can provide me a link to a tutorial / code , would be grateful. I've searched thru all that google came up for dynamic dropdowns using php and i cannot find what i need.


Thanks. Swati
since .php is only loaded once, you'd have to reload the page inbetween each selection... you'd probly want a to intermingle javascript and .php together to get something like this

[code]
<select size="1" onchange="document.location.href=this.value">
<option value="'.$PHP_SELF.'.?country='.$countryname.'">'.$countryname.'</option>
</select>

<?
switch($_GET[country]){
case "countryname":
 echo '<select size="1" onchange="document.location.href=this.value">';
 echo  '<option value="'.$PHP_SELF.'.?country='.$countryname.'">'.$countryname.'</option>';
 echo '</select>';
break;
}
?>
[/code]
or something to that effect...
JS is just not on - i'm looking at a total of some 5000 odd towns in the 2nd list. I realise using php means travelling back to the server but its still a better option.
Taith, i am veering towards using a combination of JS and PHP but since i dont know how to actually code it , i'd rather look at a done code and modify it or read some detailed tutorial on it... I did try my hand doing it with what i've read so far but since the script can run only when a value from an earlier form gets posted, its causing me a problem in where to run the js.

The bare bones of the php script is like this and the js/php needs to get inserted somewhere..any pointers ??

Thanks !

[code]<? include("protect.php"); ?>

<?php
$cid = $_GET["cid"];
echo $cid; // to confirm the value gets passed - it does


mysql_connect("localhost", $dbname, $dbpasswd )    or die ("Unable to connect to server.");

mysql_select_db($database)    or die ("Unable to select database.");


$sql1 = "SELECT * FROM `Customers` WHERE `CID`= $cid ";
$result1 = mysql_query($sql1);

$myrow1 = mysql_fetch_array($result1);

$company = $myrow1["Company"];
?>
<form enctype='multipart/form-data' action='add_custadd2.php?cid=<? echo $cid ?>&userid=<? echo $userid ?>' method='post'>
<table border="1" cellspacing="1" style="border-collapse: collapse" bordercolor="#000066" width="95%" cellpadding="5">


<tr><font face="Verdana" size="2">COMPANY</td>
    <input type=hidden name="company"  value="<? echo $company ?>">
    <input type=text name="company"  value="<? echo $company ?>" disabled="true">&nbsp;</td>

<tr>Country</td>
  <select name="ctid"><option value="">[Select One]
        <?php


  $result = mysql_query("SELECT * FROM `Countries`  ORDER BY `Country` asc ");

  if ($myrow = mysql_fetch_array($result)) {

    do {

       printf("<option value=%d>
        %s", $myrow["ctid"], $myrow["Country"]);

    } while ($myrow = mysql_fetch_array($result));

  echo "</select>\n";

  } else {

  echo "Sorry, no records were found!";

  }

  ?></td></tr>

  // JS-PHP needs to kick in at this point when selecting the 2nd dropdown
 
 
<tr>Towns</td>
  <select name="tid"><option value="">[Select One]
        <?php


  $resulta = mysql_query("SELECT * FROM `Towns`  WHERE `ctid` = $ctid ORDER BY `ctid` asc ");
  // This is the critical part where the value of country id is obtained to filter towns by country

  if ($myrowa = mysql_fetch_array($resulta)) {

    do {

       printf("<option value=%d>
        %s, %s", $myrowa["TID"], $myrowa["Town"], $myrowa["State"]);

    } while ($myrowa = mysql_fetch_array($resulta));

  echo "</select>\n";

  } else {

  echo "Sorry, no records were found!";

  }

  ?></td></tr>[/code]
how about something like this?
[code]
<? include("protect.php");
$cid = $_GET["cid"];
echo $cid;

mysql_connect("localhost", $dbname, $dbpasswd ) or die ("Unable to connect to server.");

mysql_select_db($database) or die ("Unable to select database.");

$result1 = mysql_query("SELECT * FROM `Customers` WHERE `CID`= $cid ");
$myrow1 = mysql_fetch_array($result1);

$company = $myrow1["Company"];
?>
<form enctype='multipart/form-data' action='add_custadd2.php?cid=<?=$cid?>&userid=<?=$userid?>' method='post'>
<table border="1" cellspacing="1" style="border-collapse: collapse" bordercolor="#000066" width="95%" cellpadding="5">
  <tr><font face="Verdana" size="2">COMPANY
    <td><input type=hidden name="company"  value="<?=$company?>"><input type=text name="company"  value="<?=$company?>" disabled="true">&nbsp;</td>
    <tr>Country</td><select size="1" onchange="document.location.href=this.value"><option value="">[Select One]</option>
<?
$result = mysql_query("SELECT * FROM `Countries`  ORDER BY `Country` asc ");
while($myrow = mysql_fetch_array($result)){
if($row[country]==$_GET[country]) echo '<option selected value="?country='.$myrow[country].'">'.$myrow[country].'</option>';
else echo '<option value="?country='.$myrow[country].'">'.$myrow[country].'</option>';
}
echo '</select>';
?>
  </td>
</tr>
<?if(!empty($_GET[country])){?>
<tr>
  <td>Towns<br>
  <select name="tid"><option value="">[Select One]
        <?
$resulta = mysql_query("SELECT * FROM `Towns`  WHERE `ctid` = $ctid ORDER BY `ctid` asc ");
while($myrowa = mysql_fetch_array($resulta)){
echo '<option value=%d>%s, %s".', '.$myrowa["TID"].', '.$myrowa["Town"].','. $myrowa["State"];
}
echo "</select>\n";
?>
  </td>
</tr>
<?}?>
</table>
[/code]
I hate to do this - ask yet another question but i uploaded the code and while the 1st dropdown works, on selection , it loads again so the 2nd doesnt work at all. i turned error reporting on but cannot find a reason other than the fact that $ctid is not defined. $ctid is the value of field CTID(int) linked to Country(varchar) so if Norway is chosen and is listed at no. 24, $ctid = 24.


[code]$resulta = mysql_query("SELECT * FROM `Towns`  WHERE `ctid` = $ctid ORDER BY `ctid` asc ");[/code]

I tried defining it as under but then the whole page turns blank.

[code]else echo '<option value="?country='.$myrow[country].'">'.$myrow[country].'</option>';
}
echo '</select>';

$ctid = $myrow[ctid];
echo $ctid ; // doesnt echo out.
?>[/code]

Any guidance please ? Thanks !
not a problem :-)  how about this modification?
[code]
<?
$result = mysql_query("SELECT * FROM `Countries`  ORDER BY `Country` asc ");
while($myrow = mysql_fetch_array($result)){
if($row[country]==$_GET[country]){
  $country=$myrow[country];
  echo '<option selected value="?country='.$myrow[country].'">'.$myrow[country].'</option>';
}else echo '<option value="?country='.$myrow[country].'">'.$myrow[country].'</option>';
}
echo '</select>';

echo $country;
?>
[/code]
Hello Barand,

Thanks for the link- i had seen it and baaSelect when i first started doing this dropdown but  i've been making too many errors and havent been able to find their source. I modified _d and _o to use my data but if i am not mistaken _t is for a 3rd dropdown that i dont need.

I'm having a hard time understanding how it /ajax works . The way i understood it : Every destination is a function of origin, time and price . For my code, it means, every country is a function of city and its associated province .

Having chosen a destination(country), i have to choose an origin ( city ) and time (province) . But in my table structure of Towns, i have fields cityid(int), city(varchar), pid(int), province(varchar), ctid(int) , country(varchar).
That means function getTimes and function getPrice are not required. Which means i dont need to use the inner join in_t and if i dont, then how do i get the value of $id.

I also dont understand how the 4 php files are linked because i dont see any form processing in them just in the html file. Would i have to create a folder for the html , js and the 5 php files ?

Does that make sense ?

Thanks for reading this post !


With a lot of help got this to work in some fashion. its not neat and requires the whole page to reload but with no knowledge of ajax, its the best i can do for now. have posted the code in case anyone else would like to use it.

Anyway, Taith, Barand - thanks for stopping by to help

Swati

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.