0

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;
        ?>

Edited by niths: n/a

3
Contributors
11
Replies
15
Views
7 Years
Discussion Span
Last Post by niths
0

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.

0

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.

0

Change line 11:

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

Edited by pritaeas: n/a

0

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.

0

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.

0

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.

0

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

0

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.

0

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.

0

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;
              
            }
            
           
        ?>

Edited by niths: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.