Hi all,
i had a page with a dropdown option.so wen i select an item from dropdown my page is getting refresh and in the dropdown it is cuming to default value.I need that selected value in dropdown even after the page refreshed.so can any one please...

<?php
include 'connection.php';
    $sql="SELECT projectname from projects where createdby='Jack'";
    mysql_error();
    $result=mysql_query($sql);
    $options=""; 
    while ($row=mysql_fetch_array($result)) 
    {
        $id=$row["projectid"];
        $projectname=$row["projectname"];
        $options.="<option value='".$projectname."'>".$projectname.'</option>';
    }  
    
    $sql2="SELECT * FROM users where reportingto='Jack' and role='2'";
    mysql_error();
    $result2=mysql_query($sql2);
    $num=mysql_num_rows($result2);
    $projectassign=$_POST['projectassign'];  
?>
<script type="text/javascript" language="javascript">
   function getValue(selval)
   {
          
     location.href="noname1.php?projassign="+selval;
    
   }
</script>
<html>
<body>
<form name="myform">
<select id="prolist" name="projectassign" onchange="getValue(this.value);" ><option >----select project----</option>
<?php echo $options ?> 
 </select> 
 
 <table border="1" cellspacing="0" cellpadding="0" align="center" style="width: 500px" bordercolor="red">
    <tr>
        <th></th>
        <th style="color: navy;">Userid</th>
        <th style="color: navy;">Username</th>
    </tr>
    <?php
    $sno=1;
    while($row=mysql_fetch_array($result2, MYSQL_ASSOC)){
    ?>
     <tr>
<td><input name="check_list" type="checkbox" value="<? echo $row['userid']; ?>"></td>
<td><? echo $row['userid']; ?></td>
<td><? echo $row['username'];?></td>
</tr>
<?php
    $sno=$sno+1;
}
?>
    </table>
</form>
</body>
</html>
          <?php
          $projval=$_GET['projassign'];
           $sql3="select userid from projectassign where projectassign='$projval'";
           mysql_error();
           $result3=mysql_query($sql3);
           $num1=mysql_num_rows($result3);
            $arruid=mysql_fetch_array($result3);
             $i=0;
             while ($i < $num1)
             {
                 $f1=mysql_result($result3,$i,"userid"); 
              //   echo $f1;
               echo "<script type='text/javascript' language='javascript'>
                var qval=". $f1 ." ;
                var chkarr=document.myform.check_list;
                for(i=0;i<chkarr.length;i++)
                {
                    if(chkarr[i].value==".$f1.")chkarr[i].checked=true;
                }
                </script>";
                $i++; 
                }
            if($projval!='')   
             echo "assigned users for ".$projval;
        ?>

In the loop where you build $options, you should check whether the projectname matches $_GET. If so, then add selected="selected" attribute to the option.

actually due to line 24 my page will reload. so i am getting the default value.i tried the above one but i didnt get it(if i'm not wrong). so i thought through ajax we can do. if not can u give clear view for the above code.

Change line 11:

$options .= "<option value='" . $projectname . "' " . ($projectname == $_POST['projectassign'] ? 'selected="selected"' : '') . ">" . $projectname . '</option>';

i made the changes u said but i didnt get it.After selecting dropdown list the page is reloading and the selected value is changing.

Did you try GET instead of POST ?

i tried this but i didnt get it.whenever i click on dropdown and select a project my page is reloading.due to onchange="getValue(this.value);" in line 31 and the getvalue is at line 24. so my page is getting reload.

I understand now I think. The solution would be to add the onChange event after the options are outputted. So I think you need a function that is called onLoad to do that.

how can i write that...? can u suggest me basing on my requirement.

Add something like this at the end of your document:

select = document.getElementById('prolist');
select.onchange= function() { getValue(this.value); };

Cannot test it right now, so not sure if it is correct, but it should get you going.

Member Avatar for rajarajan2017

To avoid refreshing issues you should use AJAX. or otherwise try to execute the same page by using the SELF concept. then you can read the variable you selected in a variable and even the page get refreshed you can retain the value by using GET or POST.

iam new to ajax but i tried it. i didnt get it. so is there any mistake in this code can u see this please..

<?php
include 'connection.php';
    $sql="SELECT projectname from projects where createdby='Jack'";
    mysql_error();
    $result=mysql_query($sql);
    $options=""; 
    while ($row=mysql_fetch_array($result)) 
    {
        $id=$row["projectid"];
        $projectname=$row["projectname"];
        $options.="<option value='".$projectname."'>".$projectname.'</option>';
    }  
    
    $sql2="SELECT * FROM users where reportingto='Jack' and role='2'";
    mysql_error();
    $result2=mysql_query($sql2);
    $num=mysql_num_rows($result2);
    $projectassign=$_POST['projectassign'];  
?>
<html>   
<head>
<script type="text/javascript" language="javascript"> 
 function getValue(selval)
{
     
if (selval=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange= function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
 
xmlhttp.open("GET","noname1.php?projassign="+selval,true);
xmlhttp.send();
}
function getValue(selval)
           {
                location.href="noname1.php?projassign="+selval;
           }   
</script>
</head>
<body>
<form name="myform">
<select id="prolist" name="projectassign" onchange="getValue(this.value);" >
<option >----select project----</option>
<?php echo $options ?> 
   
 </select> 
 <div id="txtHint">
 <table border="1" cellspacing="0" cellpadding="0" align="center" style="width: 500px" bordercolor="red">
    <tr>
        <th></th>
        <th style="color: navy;">Userid</th>
        <th style="color: navy;">Username</th>
    </tr>
    <?php
    $sno=1;
    while($row=mysql_fetch_array($result2, MYSQL_ASSOC)){
    ?>
     <tr>
<td><input name="check_list" type="checkbox" value="<? echo $row['userid']; ?>"></td>
<td><? echo $row['userid']; ?></td>
<td><? echo $row['username'];?></td>
</tr>

<?php
    $sno=$sno+1;
}
?>
    </table>
    
</div>
</form>
</body>
</html>  
           <?php
           $projval=$_GET['projassign'];
            
           $sql3="select userid from projectassign where projectassign='$projval'";
           mysql_error();
           $result3=mysql_query($sql3);
           $num1=mysql_num_rows($result3);
            $arruid=mysql_fetch_array($result3);
             $i=0;
             
              while ($i < $num1)
             {
                 $f1=mysql_result($result3,$i,"userid"); 
              //   echo $f1;
         
              echo "<script type='text/javascript' language='javascript'>
                var qval=". $f1 ." ;
                var chkarr=document.myform.check_list;
                for(i=0;i<chkarr.length;i++)
                {
                    
                    if(chkarr[i].value==".$f1.")chkarr[i].checked=true;
                }
                
              </script>";
              
   
              $i++; 
              
               }
            if($projval!='')  
            {
             echo "assigned users for ".$projval;
              
            }
            
           
        ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.