Vinodpv Posted April 23, 2014 Share Posted April 23, 2014 Hello freinds, Here 1st dropdown list not holding its value... CAN U HELP ME... <body><?phpinclude "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>";?><?phpif(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 Quote Link to comment https://forums.phpfreaks.com/topic/287960-once-my-page-reloads-my-first-drop-down-list-is-not-holding-the-selected-options/ Share on other sites More sharing options...
jazzman1 Posted April 23, 2014 Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287960-once-my-page-reloads-my-first-drop-down-list-is-not-holding-the-selected-options/#findComment-1477055 Share on other sites More sharing options...
Vinodpv Posted April 23, 2014 Author Share Posted April 23, 2014 I didnt get any error . I used error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/287960-once-my-page-reloads-my-first-drop-down-list-is-not-holding-the-selected-options/#findComment-1477058 Share on other sites More sharing options...
mac_gyver Posted April 23, 2014 Share Posted April 23, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287960-once-my-page-reloads-my-first-drop-down-list-is-not-holding-the-selected-options/#findComment-1477063 Share on other sites More sharing options...
fastsol Posted April 23, 2014 Share Posted April 23, 2014 I have this tutorial on the subject http://amecms.com/article/Retaining-Drop-down-Selection-After-Form-Submit Quote Link to comment https://forums.phpfreaks.com/topic/287960-once-my-page-reloads-my-first-drop-down-list-is-not-holding-the-selected-options/#findComment-1477104 Share on other sites More sharing options...
Psycho Posted April 23, 2014 Share Posted April 23, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/287960-once-my-page-reloads-my-first-drop-down-list-is-not-holding-the-selected-options/#findComment-1477121 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.