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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    populating select boxes on the basis of the choices

    I have to repopulate the select boxes
    My problem is I m having four select boxes on my page, These select are containig data which I have fetched from the database , like One is College Type other college name, third is branch name and final is city. I have to populate the every select on the basis different choice like if any one select a college type as private colleges then the colege name select should contain name of the college that are private colleges , now if click on the college name and select any particular college then the branch select should contain the branches available in that particular college, I will be very thankful if you plz me help,
    thank you
    the code is
    <? session_start();

    require_once("db.php");
    $global_dbh=mysql_connect($hostname,$username,$password ) or die("Could not connect to database");
    mysql_select_db($db,$global_dbh) or die("Could not select database");


    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Rajiv Gandhi Proudyogiki Vishwavidyalay</title>
    <style type="text/css">
    <!--
    .style1 {
    color: #993300;
    font-size: 24px;
    font-family: Arial, Helvetica, sans-serif;
    font-weight: bold;
    }
    .style2 {
    font-size: 14px;
    font-family: Arial, Helvetica, sans-serif;
    font-weight: bold;
    }
    .style3 {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 24px;
    color: #330066;
    }
    .style6 {
    font-family: Arial, Helvetica, sans-serif;
    color: #0000FF;
    font-size: 14px;
    font-weight: bold;
    }
    .style11 {font-family: Arial, Helvetica, sans-serif; font-weight: bold; color: #CC0000; }
    -->
    </style>
    </head>
    <script type="text/javascript">
    function Set2()
    {

    alert(<? $d=$_POST['CollTypeIndex']; echo "$d"; ?>);
    alert("" + document.form1.CollTypeIndex.value);
    document.form1.CollIdIndex.value=document.form1.select1.selectedIndex;
    document.form1.SetCollege.value="1";
    document.form1.CollId.value=document.form1.select1.value;
    document.form1.submit();
    }

    function Set1()
    {



    document.form1.CollTypeIndex.value=document.form1.select0.selectedIndex
    document.form1.SetType.value="1";
    if(document.form1.select0.selectedIndex==0)
    {
    document.form1.Type.value="3";
    }
    else
    {
    document.form1.Type.value="1";
    }
    //alert(document.form1.Type.value);
    document.form1.submit();
    }
    </script>


    <body BGCOLOR="#CCDDEE" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
    <form name="form1" action="" onSubmit="return Validate()" method="post" >
    <?
    if(!isset($_POST['Submit']))
    {
    ?>
    <input name="CollId" type="hidden" value="0">
    <input type="hidden" name="CollTypeIndex">
    <input type="hidden" name="CollIdIndex">
    <input type="hidden" name="BranchIndex">
    <input type="hidden" name="CityIndex">
    <input type="hidden" name="CollId">
    <input type="hidden" name="BranchId">
    <input type="hidden" name="City">
    <input type="hidden" name="Type">
    <input type="hidden" name="SetType" >
    <input type="hidden" name="SetCollege" >
    <input type="hidden" name="SetBranch" >
    <input type="hidden" name="SetCity" >
    <?
    }
    ?>


    <table width="100%" border="1" cellpadding="0" cellspacing="0">
    <!--DWLayoutTable-->
    <tr>
    <td width="124" height="30" align="center" valign="middle"><span class="style11">College Type </span></td>
    <td width="298" align="center" valign="middle"><span class="style11">College Name </span></td>
    <td width="194" align="center" valign="middle"><span class="style11">Branch</span></td>
    <td width="165" height="30" align="center" valign="middle"><span class="style11">City</span></td>
    </tr>
    <tr>
    <td height="35" align="center" valign="middle"><select name="select0" onChange="Set1()">
    <option>Private</option>
    <option>Govenment</option>

    </select></td>
    <td height="35" align="center" valign="middle"><span class="style2">
    <select name="select1" onChange="Set2()">
    <?
    if($_POST['SetType']==1)
    { echo "Hello";
    $colltype=$_POST['Type'];
    $sql= "SELECT distinct CollegeMaster.ShortName FROM CollegeBranchMaster INNER JOIN CollegeMaster ON CollegeBranchMaster.CollegeId = CollegeMaster.CollegeId WHERE (((CollegeMaster.CollType)=$colltype)"; // );
    if($_POST['SetBranch']==1)
    {
    $br=$_POST['BranchId'];
    $sql=$sql." AND ((CollegeBranchMaster.BranchId)=\"$br\") ";
    }
    if($_POST['SetCity']==1)
    {
    $city=$_POST['City'];
    $sql=$sql." AND ((CollegeMaster.City)=\"$city\")";

    }
    $sql=$sql." );";
    $CollType=$_POST['CollTypeIndex'];
    echo "<script languauge='Javascript'>\n
    document.form1.select0.selectedIndex=$CollType;</script>";
    }
    else
    {
    $sql="Select ShortName from CollegeMaster where CollType=3 order by CollegeId;";


    }
    echo "$sql";
    $result=mysql_query($sql, $global_dbh) or die("display_db_query:" . mysql_error());
    while($r=mysql_fetch_row($result))
    {
    echo "<option>$r[0]</option>";
    }
    ?>
    </select>
    </span></td>
    <td height="35" align="center" valign="middle"><span class="style2"> </span><span class="style2">

    <select name="select3">
    <?
    if($_POST['SetType']==1)
    { echo "Hello";
    $colltype=$_POST['Type'];
    $sql= "SELECT distinct CollegeBranchMaster.BranchId FROM CollegeBranchMaster INNER JOIN CollegeMaster ON CollegeBranchMaster.CollegeId = CollegeMaster.CollegeId WHERE (((CollegeMaster.CollType)=$colltype)"; // );
    if($_POST['SetCollege']==1)
    {
    $Coll=$_POST['CollIdIndex'];
    echo "<script languauge='Javascript'>\n
    document.form1.select1.selectedIndex=$Coll;</script>";
    $cl=$_POST['CollId'];
    $sql=$sql." AND ((CollegeBranchMaster.CollegeId)=\"$cl\") ";
    }
    if($_POST['SetCity']==1)
    {
    $city=$_POST['City'];
    $sql=$sql." AND ((CollegeMaster.City)=\"$city\")";

    }
    $sql=$sql." );";
    $CollType=$_POST['CollTypeIndex'];
    echo "<script languauge='Javascript'>\n
    document.form1.select0.selectedIndex=$CollType;</script>";
    }
    else
    {
    $sql="Select BranchId from BranchMaster order by BranchId;";


    }
    echo "$sql";
    $result=mysql_query($sql, $global_dbh) or die("display_db_query:" . mysql_error());
    while($r=mysql_fetch_row($result))
    {
    echo "<option>$r[0]</option>";
    }
    ?>
    </select>
    </span></td>
    <td height="35" align="center" valign="middle"><select name="select4"><? $query="Select distinct City from CollegeMaster order by City;";
    $result=mysql_query($query, $global_dbh) or die("display_db_query:" . mysql_error());
    while($r=mysql_fetch_row($result))
    {
    echo "<option>$r[0]</option>";
    }
    ?>

    </select></td>
    </tr>
    <tr>
    <td height="30" colspan="4" align="center" valign="middle"><span class="style2">
    <input type="submit" name="Submit" value="Submit">
    </span></td>
    </tr>

    </table>

    </form>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It sounds like you want a solution to dynamically change the contents of one select box based on the selection made in a separate box?

    To do this you will need to use javascript. It's possibly through PHP but not without refreshing the page each time you make a selection and I don't think that's what you want.

    I recommend you ask in the javascript forum unless you actually DO want to reload the page after each selection and re-populate the forms with $_POST data?


  •  

    Posting Permissions

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