MY Code is:-
<?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">
function setVal()
{
//var sel = document.getElementById("branch");
//var val = select.options[select.selectedIndex].value;
//sel.options[country.options.selectedIndex].setAttribute("selected", val);
//var select = document.getElementById("branch");
//var val = select.options[select.selectedIndex].value;//getting value
//alert(val);
//document.getElementsByName("branch")[1].selectedIndex = val;
//return val;
}
</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();setVal();">
<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'];
function set()
{
return $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>
<tr><td>Submit<td><input type="submit" name="process" id="process" value="Process"></td> </tr>
</table>
</form>
</body>
</html>
<?php
mysqli_close($dbcon);
?>
i want to retain value of select tag whose id=branch after submitting form by selecting option