SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: Three dropdown

  1. #1
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Three dropdown

    hi
    I know that is not so difficult but......
    I have a page where there are three dropdown ,I want to populate these by depending on eachother
    like 2nd one depend on 1st and 3rd one depend on 2nd.

    I have the following code , i am trying to solve it but no luck.

    Code:
    PHP Code:
    <html>

    <head>
    <title>Multiple drop down list box from plus2net</title>
    <meta name="GENERATOR" content="Arachnophilia 4.0">
    <meta name="FORMATTER" content="Arachnophilia 4.0">
    <SCRIPT language=JavaScript>
    function reload(form)
    {
    var val=form.cat.options[form.cat.options.selectedIndex].value;
    self.location='dd.php?cat=' + val ;
    }
    </script>
    <SCRIPT language=JavaScript>
    function reload(sub)
    {
    var val=sub.subcat.options[sub.subcat.options.selectedIndex].value;
    self.location='dd.php?subcat=' + val ;
    }
    </script>
    </head>

    <body>
    <?
    @$cat=$_GET['cat']; // Use this line or below line if register_global is off
    @$subcat=$_GET['subcat'];
    ///////// Getting the data from Mysql table for first list box//////////
    $quer=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category");
    ///////////// End of query for first list box////////////
    /////// for second drop down list we will check if category is selected else we will display all the subcategory/////
    if(isset($cat) and strlen($cat) > 0){
    $quer2=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory");
    }else{
    $quer2=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); }

    // third dropdown
    if(isset($subcat) and strlen($subcat) > 0){
    $quer3=mysql_query("SELECT DISTINCT name FROM sub_cat where id=$subcat order by name");
    }else{
    $quer3=mysql_query("SELECT DISTINCT name FROM sub_cat order by name"); }

    echo 
    "<form method=post name=f1 action='dd.php'>";

    echo 
    "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
    while(
    $noticia mysql_fetch_array($quer)) {
    if(
    $noticia['cat_id']==@$cat){echo "<option selected value='$noticia[cat_id]'>$noticia[category]</option>"."<BR>";}
    else{echo  
    "<option value='$noticia[cat_id]'>$noticia[category]</option>";}
    }
    echo 
    "</select>";

    echo 
    "<select name='subcat' onchange=\"reload(this.sub)\"><option value=''>Select one</option>";
    while(
    $noticia2 mysql_fetch_array($quer2)) {
    //if($noticia['id']==@$subcat){echo "<option selected value='$noticia2[id]'>$noticia2[subcategory]</option>"."<BR>";}
    //else{
    echo  "<option value='$noticia2[subcategory]'>$noticia2[subcategory]</option>";}

    echo 
    "</select>";

    echo 
    "<select name='sub'><option value=''>Select one</option>";
    while(
    $noticia3 mysql_fetch_array($quer3)) {
    echo  
    "<option value='$noticia3[id]'>$noticia3[name]</option>";
    }
    echo 
    "</select>";

    echo 
    "<input type=submit value=Submit>";
    echo 
    "</form>";
    ?>
    </body>
    </html>
    any help will be aprriciated.
    thanks.

  2. #2
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    before using this code...i want to remind that this code need refresh every time the option is selected which seems very very unpro and hazardous for big site

    just a note...
    i have found another,rather remixed good ajax for doing it..for 2 but have not test for three

  3. #3
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use AJAX or put each drop-down in a spearate iframe and then reload an iframe but not the whole page.

  4. #4
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for ur reply.
    Can u give some url for this using ajax.

    And iframe(for three dropdown).

    Thanks

  5. #5
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is an AJAX tutoiral - http://www.sitepoint.com/article/bui...-ajax-web-apps. I believe you can find more on SitePoint.

    As for iframes, I've never seen a tutorial on this method, I just used it .

    The idea is that you have an iframe that points to a PHP script that gets a parameter and returns a <select> according to that parameter.

    For example, you have two fields: country and location. The location field is placed in to the iframe, when changing the value in the country field you change the URL of that iframe by using JavaScript.

  6. #6
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.
    I will take a look on these.

  7. #7
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would recommend you to use AJAX, it's an elegant and modern solution.

  8. #8
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ajax...

    It means, i totally new to this. is it complex or easy to use and learn.

    I am good in php, html,js.

    I read some articles about ajax before, but that time i did not need it so i did not
    give it more response.

    Can u give some more clear view for ajax.

  9. #9
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AJAX is just a way of interacting between JavaScript and server-side scripts - PHP or whatever. So you don't have to learn a new langauge. You just need to learn some new JavaScript functions.

    Here is a good tutorial and explanation - http://w3schools.com/ajax/default.asp

  10. #10
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Mike Borozdin for ur help.


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
  •