-1

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

j08691
  • 204,283
  • 31
  • 260
  • 272
  • 3
    **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 10 '14 at 13:59
  • 3
    Danger: you are vulnerable to bad coding practice. That code looks messy and difficult to maintain. http://en.wikipedia.org/wiki/Best_coding_practices – Husman Jun 10 '14 at 14:01
  • You could use PDO prepared statements for prevention against SQL injection attacks – Prashant Jun 14 '14 at 11:31

2 Answers2

1

You could also use AJAX for form submission without reloading the page.

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>   
<script type="text/javascript">
function submit(){
      get all values in javascript variables with id's or class
      /* accessing value by id */

      var branch = $("#branch").val();
      $.post("./",{branch: branch}, function(data){
           handle response from your script
      });

}
</script>
Prashant
  • 2,005
  • 3
  • 17
  • 24
0

First, you should work hard on making your codes easier to read (maintenance work) and also to organize it all differently. Like this, put all the PHP first and then all the HTML. Don't mess it all up.

Second, in the form tag, leave action="" just like that, nothing between the quotes, that will send the form to the same page. And use double quotes.

Third, in PHP, all you have to do is this:

<?php
$branch = $_POST['branch'];
$subcode = $_POST['subcode']; // between these single quotes, put the name attribute for the select tag you want, in this case: branch and subcode.
?>

And then just use the variables as you need them!

Then, if you want to still keep those variables when reloading after submitting... even tho $_POST also works and I don't know why it's not for you, you might be looking for $_GET.

So set the next URL to be something like:

<form action="/whatever/index.php?branch=<?php echo $branch; ?>&subcode=<?php echo $subcode; ?>">

So then use $_GET['branch']; and $_GET['subcode']; to get those values again.

Remember to use the variables before the HTML form tag because if not, then your action won't use the correct values to retrieve with $_GET.

Zeke
  • 1,281
  • 1
  • 18
  • 26