1
<?php
include_once 'database_connect.php';
$conn = new dbconnection();
$dbcon = $conn->connect();
if (!$dbcon) {
    die("Fail".mysqli_error($dbcon));
}
?>
<html>
<head>
    <title></title>
    <script type="text/javascript"></script>
</head>
<body>
<form name="frm" method="post" action='<?php echo $_SERVER['PHP_SELF']; ?>'>
    <table width="50%" border="1" cellpadding="3" cellspacing="3" align="center">
        <?php
        $value1 = array();
        $select_query = "SELECT Distinct branch FROM subjects";
        $result = mysqli_query($dbcon, $select_query);
        if (!$result) {
            die("Fail".mysqli_error($dbcon));
        }

        while ($row = mysqli_fetch_array($result)) {
            $value1[] = $row['branch'];
        }

        ?>
        <tr>
            <td>Branch
            <td><select name="branch" id="branch" onchange="document.frm.submit();">
                    <option>Select Branch</option>
                    <?php
                    foreach ($value1 as $gets)
                        echo "<option value={$gets}>{$gets}</option>";
                    ?>
                </select>

                <?php
                $value2 = array();
                if (isset($_POST['branch'])) {
                    $branch = $_POST['branch'];
                    $getsub_query = "SELECT sub_code FROM subjects where branch='$branch'";
                    $result2      = mysqli_query($dbcon, $getsub_query);
                    if (!$result2) {
                        die("Fail\n".mysqli_error($dbcon));
                    }
                    while ($row1 = mysqli_fetch_array($result2)) {
                        $value2[] = $row1['sub_code'];
                    }
                }
                ?>
        <tr>
            <td>Subject Code
            <td><select name="subcode" id="subcode">
                    <option>Subject Code</option>
                    <?php
                    foreach ($value2 as $gets)
                        echo "<option value={$gets}>{$gets}</option>";
                    ?>
                </select>

This code gets the first drop down list branch from data base. When we select value from it, second drop down list get filled from database . but problem is when i select option in first drop down list, the selected option does not remain their in first drop down list . but second drop down list fills correctly. i want that option i have selected should remain selected. Like its state should be changed. I think first drop down list gets filled again on form load.

colburton
  • 4,559
  • 2
  • 21
  • 36
  • On a side note, you should not write sql requests as "SELECT sub_code FROM subjects where branch='$branch'" ; There is a huge security issue with this. if $branch = "';DELETE FROM subjects WHERE '' = '" it could wipe out your table. Look up "SQL Injection" for further information – Jerome WAGNER Jun 17 '14 at 09:44
  • 1
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jun 17 '14 at 12:40

1 Answers1

1

You have to mention in your html that the option is indeed selected.

Try replacing

echo "<option value={$gets}>{$gets}</option>";

By

$selected = '';
if (isset($_POST['branch'] && $gets==$_POST['branch']) {
  $selected = ' selected="selected"';
}
echo "<option value={$gets}".$selected.">{$gets}</option>";
Jerome WAGNER
  • 20,021
  • 6
  • 55
  • 73