how to retain dropdown list value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mypopy
Forum Newbie
Posts: 3
Joined: Thu Nov 09, 2006 7:24 am

how to retain dropdown list value

Post by mypopy »

Hi everyone

Anyone can help?

What I am trying to do is:

I have a s dropdown list on the first page. When I select from the first drop down is not retaining the value,

I want dropdown list retain the chosen values . How can I do that?

Thanks for your help.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Inject "selected" or "selected="selected"" into the option that was .. selected.
mypopy
Forum Newbie
Posts: 3
Joined: Thu Nov 09, 2006 7:24 am

how to retain dropdown list value

Post by mypopy »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


HI I tried but still not working here is my coding
beginning of Store_locator

Code: Select all

<?php

        require_once('z3.php'); 
        $Province               =getProvince($Data); 
        $City                   =getCities($Data);
        $Store                  =getStores($Data);
   // die(print_r($_POST));
        if (isset($HTTP_POST_VARS['store_locator']))
        { 


        $ID=$_POST['cityid'];
           $Sql="SELECT * FROM store where City_ID='".$ID."' order by Store_Name";
           $Res=mysql_query($Sql);
        //echo $Sql;
             $Str="<table><tr><td class='text' >Store Name</td><td class='text' >Location</td><td class='text' >Contact No</td></tr>";
        while ($Rw=mysql_fetch_array($Res))
        {
        $Str.="<tr>
        <td class='text' >".$Rw['Store_Name']."</td>
        <td class='text'>".$Rw['Store_Location']."</td>
        <td class='text'>".$Rw['Contact_No']."</td>
        </tr>";
        }
        $Str.="<tr><td><input type='submit' value='back' name='back' onClick='javascript:history.back(1)' ></td> </tr>";
        
        /*die($Province); 
        die($City );*/
        echo $Str;
        }
    
        function getProvince($Data=array(),$ProvinceID="")
        {
         //die(print_r($_POST));
        
           $Sql="SELECT * FROM province order by Province_Name";
           $Res=mysql_query($Sql);
           //print mysql_num_rows($Res);
                
                 $Str="<select name='province_id' class='inputbox' onchange=\"reload(this.form)\"  >";
                $Str.=" <option value=''> Select Province</option>";
         
                while ($Rw=mysql_fetch_array($Res))
                { 
                $Selected="";
                        if (($Data['province_id']==$Rw['Province_ID']) || ($Rw['Province_ID']==$ProvinceID))
                         {
                        $Selected="selected";
                        //die($Data['province_id']);
                         }
        
                        $Str.="<option value=".$Rw['Province_ID']." ".$Selected.">".$Rw['Province_Name']."</option>";
                        
                }


                $Str.="</select>";
                return $Str;
        }
        
        
        function getCities($Data=array(),$ProvinceID="",$CityID="")
        {

        $ID=$_GET['province_id'];
           $Sql="SELECT * FROM city where Province_ID='".$ID."'
                order by City_Name";
        
           $Res=mysql_query($Sql);
          // print mysql_error();
           
                $Str="<select name='cityid' class='inputbox'>";
                $Str.="<option value='0'> Select a City</option>";
                while ($Rw=mysql_fetch_array($Res))
                { 
//                      print '<br>xx '.$Rw['City_ID'];
                        
                        $Str.="<option value=".$Rw['City_ID']." ".$Selected.">".$Rw['City_Name']."</option>";
                }
                $Str.="</select>";
                return $Str;
        }
        
        
function getStores($Data=array(),$CityID="",$StoreID="")
        {   
        
   $ID=$_POST['cityid'];
           $Sql="SELECT * FROM store where City_ID='".$ID."' order by Store_Name";
           $Res=mysql_query($Sql);
        //echo $Sql;
             $Str="<table width='100%' cellpadding='0' cellspacing='0'><tr id class ='text'><td>Store Name</td><td>Location</td><td>Contact No</td></tr>";
        
                while ($Rw=mysql_fetch_array($Res))
        {
        $Str.="<tr>
        <td>".$Rw['Store_Name']."</td>
        <td>".$Rw['Store_Location']."</td>
        <td>".$Rw['Contact_No']."</td>
        </tr>";
        }
        $Str.="</table>";
        return $Str;
        }

 ?>

<html>

<head>
<title>Store Locator</title>
<link rel='stylesheet' type='text/css' href='../../templates/organic_fusion_1.0/css/template_css.css'>

</head>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.province_id.options[form.province_id.options.selectedIndex].value;
self.location='store_locator.php?province_id=' + val ;
}
function validateForm(objForm)
{
        var returnStatus = 1;

        if (objForm.Make.selectedIndex == 0) {
                alert("Please select a City");
                returnStatus = 0;
        };

        if (returnStatus) {
                objForm.submit();
        }
}
        function validateForm(objForm)
{
        var returnStatus = 1;

        if (objForm.Make.selectedIndex == 0) {
                alert("Please select a car make");
                returnStatus = 0;
        };

        if (returnStatus) {
                objForm.submit();
        }
}
 
        function Validate(form)
        {
        if (document.store_locator.cityid.value == '0') {
          alert("Please provide us with a selection.\n");
          return false;
     }
        }

</script>
</head>
<body>
         
        <form method=post name=store_locator action='locator.php' method='post' enctype='multipart/form-data'  METHOD="POST" onSubmit="return Validate(this);">
         <tr><td>Please select a province followed by a city then press submit to view the stores in that area.<br><br>



 </td></tr>
        <tr>
                
                <td><?php echo $Province?></td>
                <td><?php echo $City?></td>
                <td><input type="submit" value="submit" name="store_locator"  class='button' onClick="validateForm(document.store_locator)" ></td> 
        </tr>

        </form>
         
</body>
</html>
 
end of Store_locator

________________________________________________________________________________________________________________________
beginning of locator

 <?

require_once('z3.php'); 

        
        /*die($Province); 
        die($City );*/
         
        $stores=getStores($Data);
        function getStores($Data=array(),$CityID="",$StoreID="")
        {  
        
         
        //sql to select city
        $CID=$_POST['cityid'];
         $Cty="SELECT * FROM city where City_ID='".$CID."' order by City_Name";
          $RCty=mysql_query($Cty);
        
        while ($RwCty=mysql_fetch_array($RCty))
        {
        echo "<table align='center' class='componentheading'><tr><td>Stores in ".$RwCty['City_Name']."</td></tr></table>";
        }
        
        $ID=$_POST['cityid'];
           $Sql="SELECT * FROM store where City_ID='".$ID."' order by Store_Name";
           $Res=mysql_query($Sql);
        //echo $Sql;
        $Str="<table width='80%'cellpadding='0' cellspacing='0' class='componentheading'><tr><td>Store Name</td><td>Location</td><td>Contact No</td></tr>";
        while ($Rw=mysql_fetch_array($Res))
        {
        $Str.="<tr>
        <td class='text' >".$Rw['Store_Name']."</td>
        <td class='text'>".$Rw['Store_Location']."</td>
        <td class='text'>".$Rw['Contact_No']."</td>
        </tr>";
        }
        
        $Str.="<tr><td><input type='submit' value='back' name='back' onClick='javascript:history.back(1)' class='back_button'></td> </tr>";
        
        /*die($Province); 
        die($City );*/
         
        return $Str;
        }

?>
<html>

<head>
<title>Store Locator</title>
<link rel='stylesheet' type='text/css' href='../../templates/organic_fusion_1.0/css/template_css.css'>

</head>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.province_id.options[form.province_id.options.selectedIndex].value;
self.location='store_locator.php?province_id=' + val ;
}

</script>
</head>



<body>
         
        <form method=post name=store_locator action='store_locator.php' method='post' enctype='multipart/form-data'>
         
        <tr>
                <td class='text'><?php echo $stores?></td>
                 
        </tr>

        </form>
         
</body>
</html>
End of locator

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

cant....read....code.....must...use....php tags

Code: Select all

<?php

        require_once('z3.php'); 
        $Province               =getProvince($Data); 
        $City                   =getCities($Data);
        $Store                  =getStores($Data);
   // die(print_r($_POST));
        if (isset($HTTP_POST_VARS['store_locator']))
        { 


        $ID=$_POST['cityid'];
           $Sql="SELECT * FROM store where City_ID='".$ID."' order by Store_Name";
           $Res=mysql_query($Sql);
        //echo $Sql;
             $Str="<table><tr><td class='text' >Store Name</td><td class='text' >Location</td><td class='text' >Contact No</td></tr>";
        while ($Rw=mysql_fetch_array($Res))
        {
        $Str.="<tr>
        <td class='text' >".$Rw['Store_Name']."</td>
        <td class='text'>".$Rw['Store_Location']."</td>
        <td class='text'>".$Rw['Contact_No']."</td>
        </tr>";
        }
        $Str.="<tr><td><input type='submit' value='back' name='back' onClick='javascript:history.back(1)' ></td> </tr>";
        
        /*die($Province); 
        die($City );*/
        echo $Str;
        }
    
        function getProvince($Data=array(),$ProvinceID="")
        {
         //die(print_r($_POST));
        
           $Sql="SELECT * FROM province order by Province_Name";
           $Res=mysql_query($Sql);
           //print mysql_num_rows($Res);
                
                 $Str="<select name='province_id' class='inputbox' onchange=\"reload(this.form)\"  >";
                $Str.=" <option value=''> Select Province</option>";
         
                while ($Rw=mysql_fetch_array($Res))
                { 
                $Selected="";
                        if (($Data['province_id']==$Rw['Province_ID']) || ($Rw['Province_ID']==$ProvinceID))
                         {
                        $Selected="selected";
                        //die($Data['province_id']);
                         }
        
                        $Str.="<option value=".$Rw['Province_ID']." ".$Selected.">".$Rw['Province_Name']."</option>";
                        
                }


                $Str.="</select>";
                return $Str;
        }
        
        
        function getCities($Data=array(),$ProvinceID="",$CityID="")
        {

        $ID=$_GET['province_id'];
           $Sql="SELECT * FROM city where Province_ID='".$ID."'
                order by City_Name";
        
           $Res=mysql_query($Sql);
          // print mysql_error();
           
                $Str="<select name='cityid' class='inputbox'>";
                $Str.="<option value='0'> Select a City</option>";
                while ($Rw=mysql_fetch_array($Res))
                { 
//                      print '<br>xx '.$Rw['City_ID'];
                        
                        $Str.="<option value=".$Rw['City_ID']." ".$Selected.">".$Rw['City_Name']."</option>";
                }
                $Str.="</select>";
                return $Str;
        }
        
        
function getStores($Data=array(),$CityID="",$StoreID="")
        {   
        
   $ID=$_POST['cityid'];
           $Sql="SELECT * FROM store where City_ID='".$ID."' order by Store_Name";
           $Res=mysql_query($Sql);
        //echo $Sql;
             $Str="<table width='100%' cellpadding='0' cellspacing='0'><tr id class ='text'><td>Store Name</td><td>Location</td><td>Contact No</td></tr>";
        
                while ($Rw=mysql_fetch_array($Res))
        {
        $Str.="<tr>
        <td>".$Rw['Store_Name']."</td>
        <td>".$Rw['Store_Location']."</td>
        <td>".$Rw['Contact_No']."</td>
        </tr>";
        }
        $Str.="</table>";
        return $Str;
        }

 ?>

<html>

<head>
<title>Store Locator</title>
<link rel='stylesheet' type='text/css' href='../../templates/organic_fusion_1.0/css/template_css.css'>

</head>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.province_id.options[form.province_id.options.selectedIndex].value;
self.location='store_locator.php?province_id=' + val ;
}
function validateForm(objForm)
{
        var returnStatus = 1;

        if (objForm.Make.selectedIndex == 0) {
                alert("Please select a City");
                returnStatus = 0;
        };

        if (returnStatus) {
                objForm.submit();
        }
}
        function validateForm(objForm)
{
        var returnStatus = 1;

        if (objForm.Make.selectedIndex == 0) {
                alert("Please select a car make");
                returnStatus = 0;
        };

        if (returnStatus) {
                objForm.submit();
        }
}
 
        function Validate(form)
        {
        if (document.store_locator.cityid.value == '0') {
          alert("Please provide us with a selection.\n");
          return false;
     }
        }

</script>
</head>
<body>
         
        <form method=post name=store_locator action='locator.php' method='post' enctype='multipart/form-data'  METHOD="POST" onSubmit="return Validate(this);">
         <tr><td>Please select a province followed by a city then press submit to view the stores in that area.<br><br>



 </td></tr>
        <tr>
                
                <td><?php echo $Province?></td>
                <td><?php echo $City?></td>
                <td><input type="submit" value="submit" name="store_locator"  class='button' onClick="validateForm(document.store_locator)" ></td> 
        </tr>

        </form>
         
</body>
</html>
 
end of Store_locator

________________________________________________________________________________________________________________________
beginning of locator

 <?

require_once('z3.php'); 

        
        /*die($Province); 
        die($City );*/
         
        $stores=getStores($Data);
        function getStores($Data=array(),$CityID="",$StoreID="")
        {  
        
         
        //sql to select city
        $CID=$_POST['cityid'];
         $Cty="SELECT * FROM city where City_ID='".$CID."' order by City_Name";
          $RCty=mysql_query($Cty);
        
        while ($RwCty=mysql_fetch_array($RCty))
        {
        echo "<table align='center' class='componentheading'><tr><td>Stores in ".$RwCty['City_Name']."</td></tr></table>";
        }
        
        $ID=$_POST['cityid'];
           $Sql="SELECT * FROM store where City_ID='".$ID."' order by Store_Name";
           $Res=mysql_query($Sql);
        //echo $Sql;
        $Str="<table width='80%'cellpadding='0' cellspacing='0' class='componentheading'><tr><td>Store Name</td><td>Location</td><td>Contact No</td></tr>";
        while ($Rw=mysql_fetch_array($Res))
        {
        $Str.="<tr>
        <td class='text' >".$Rw['Store_Name']."</td>
        <td class='text'>".$Rw['Store_Location']."</td>
        <td class='text'>".$Rw['Contact_No']."</td>
        </tr>";
        }
        
        $Str.="<tr><td><input type='submit' value='back' name='back' onClick='javascript:history.back(1)' class='back_button'></td> </tr>";
        
        /*die($Province); 
        die($City );*/
         
        return $Str;
        }

?>
<html>

<head>
<title>Store Locator</title>
<link rel='stylesheet' type='text/css' href='../../templates/organic_fusion_1.0/css/template_css.css'>

</head>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.province_id.options[form.province_id.options.selectedIndex].value;
self.location='store_locator.php?province_id=' + val ;
}

</script>
</head>



<body>
         
        <form method=post name=store_locator action='store_locator.php' method='post' enctype='multipart/form-data'>
         
        <tr>
                <td class='text'><?php echo $stores?></td>
                 
        </tr>

        </form>
         
</body>
</html>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What & how is it not working? Could you post a smaller, more relevant section of code so we don't have to wade through 3 pages of possible irrelevant stuff?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply