Jump to content

Once my page reloads, my first drop down list is not holding the selected options.


Vinodpv

Recommended Posts

Hello freinds, Here 1st dropdown list not holding its value... CAN U HELP ME...

 

<body>
<?php
include "db_conexn.php";

?>
<form method="post"  name="admin">
<select name="value1" onChange="this.form.submit();"> // 1Ste Drop Down Menu Starts here
<?php
$result = mysql_query("SELECT distinct(comp_name) from company");
echo("<option value=''>---Select Company Name---</option>");
if(mysql_num_rows($result)) {
while($row = mysql_fetch_row($result))
{
echo("<option value='$row[0]'>$row[0]</option>");
}
} else {
echo("<option value=''>No</option>");
}
echo "</select>";
?>
<?php
if(isset($_POST['value1'])){ // Checks if 1Ste Drop Down menu has a value.
$value1=$_POST['value1'];
?>
<select name="value2"> //2nd Drop Down Menu Starts here
<?php
$result = mysql_query("SELECT distinct(branch) from branch where comp_name='$value1'");
if(mysql_num_rows($result)) {
while($row = mysql_fetch_row($result))
{
echo("<option value='$row[0]'>$row[0]</option>");
}
} else {
echo("<option value=''>No Data</option>");
}
echo "</select>";
?>
<?php
}else{
echo "\n";
}
?>
</form>
</body>

drop.php

Some actual errors would help. As for debugging application server's error you need to turn on php error_reporting function, for the mysql server errors use mysql_error() and read up the pink warning message box when you open the link. Use the forum's code [ code ] tags when providing code in the future.

you also need to set display_errors to ON (you should set error_reporting/display_errors in your php.ini on your development system so that you don't need to remember to put the settings into your code during development and remember to remove them when you put your code onto a live server.)

 

the reason your select menu doesn't remember the selected value is because you don't have any code to cause it to remember. as you are outputting the <option ...> tags, you must output the selected keyword in the correct <option ...> tag that you want to be selected. to do this you would test if the $_POST['value1'] isset and has the same value as the $row[0] value.

What makes you think the fields should retain their value when the page reloads? Web pages are stateless. They don't know anything about the previous page unless you code them to do so. When you build the page you need to take the POST value (if sent) to make the appropriate option selected when you build the list

 

 

<?php
 
//Get selected values
$selectedCompany = isset($_POST['company']) ? $_POST['company'] : false;
$selectedBranch = isset($_POST['branch']) ? $_POST['branch'] : false;
 
//Connect to DB
include "db_conexn.php";
 
//Create company options
$query = "SELECT distinct(comp_name) from company";
$result = mysql_query($query);
$companyOptions = "<option value=''>---Select Company Name---</option>";
while($row = mysql_fetch_assoc($result))
{
    $selected = ($row['comp_name']==$selectedCompany) ? ' selected="selected"' : '' ;
    $companyOptions .= "<option value='{$row['comp_name']}'{$selected}>{$row['comp_name']}</option>\n";
}
 
//Create branch options
$company = mysql_real_escape_string($selectedCompany);
$query = "SELECT distinct(branch) from branch where comp_name='$company'";
$result = mysql_query($query);
$branchyOptions = "<option value=''>---Select Company Name---</option>";
if(!mysql_num_rows($result))
{
    $branchyOptions .= "<option value=''>No Data</option>";
}
while($row = mysql_fetch_assoc($result))
{
    $selected = ($row['branch']==$selectedBranch) ? ' selected="selected"' : '' ;
    $branchyOptions .= "<option value='{$row['branch']}'{$selected}>{$row['branch']}</option>\n";
}
 
?>
<body>
<?php
 
?>
<form method="post"  name="admin">
    <select name="company" onChange="this.form.submit();">
    <?php echo $companyOptions; ?>
    </select>
 
    <select name="branch"> //2nd Drop Down Menu Starts here
    <?php echo $branchyOptions; ?>
    </select>
</form>
</body>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.