suyesh.amatya Posted August 5, 2008 Share Posted August 5, 2008 <form name="houston" action="" method="post"> <select name="houston" onchange="this.form.submit();"> <option value="">--Houston Criminal Attorney--</option> <? include('../include/cls_blog.php'); $objBlog=new blog; $result=$objBlog->selectAllSites(); while($rows=mysql_fetch_array($result)){ ?> <option value="<?=$rows[site]?>"><? echo $rows[site];?></option> <? } ?> </select> </form> The select box has been populated by the values from database ie $rows[site]. Now on posting the form I want to preserve the post value which is selected.How can I do this in the above code. Link to comment https://forums.phpfreaks.com/topic/118228-solved-how-to-retain-the-post-value/ Share on other sites More sharing options...
JasonLewis Posted August 5, 2008 Share Posted August 5, 2008 Well, you need to check if the current value in the loop is equal to the selected. Something like this perhaps: <form name="houston" action="" method="post"> <select name="houston" onchange="this.form.submit();"> <option value="">--Houston Criminal Attorney--</option> <?php include('../include/cls_blog.php'); $selected = ""; $objBlog=new blog; $result=$objBlog->selectAllSites(); while($rows=mysql_fetch_array($result)){ if(isset($_POST['houston'])){ //They have submitted the form if($rows['site'] == $_POST['houston']){ $selected = "selected='selected'"; }else{ $selected = "selected='selected'"; } }else{ //Not submitted, no default selection $selected = ""; } ?> <option value="<?php echo $rows['site']; ?>" <?php echo $selected; ?>><?php echo $rows['site']; ?></option> <?php } ?> </select> </form> Good luck. Link to comment https://forums.phpfreaks.com/topic/118228-solved-how-to-retain-the-post-value/#findComment-608442 Share on other sites More sharing options...
suyesh.amatya Posted August 5, 2008 Author Share Posted August 5, 2008 <form name="houston" action="" method="post"> <select name="houston" onchange="this.form.submit();"> <option value="">--Houston Criminal Attorney--</option> <? include('../include/cls_blog.php'); $objBlog=new blog; $result=$objBlog->selectAllSites(); while($rows=mysql_fetch_array($result)){ ?> <option value="<?=$rows[site]?>" selected="<? if($_POST[houston]==$rows[site]) echo "selected"; else echo "";?>"><? echo $rows[site]?></option> <? } ?> </select> </form> Now it always displays the last select option Link to comment https://forums.phpfreaks.com/topic/118228-solved-how-to-retain-the-post-value/#findComment-608446 Share on other sites More sharing options...
JasonLewis Posted August 5, 2008 Share Posted August 5, 2008 You can't echo a blank value inside the selected='' It doesn't work like that. Take a closer look at my code. Link to comment https://forums.phpfreaks.com/topic/118228-solved-how-to-retain-the-post-value/#findComment-608452 Share on other sites More sharing options...
suyesh.amatya Posted August 5, 2008 Author Share Posted August 5, 2008 <option value="<?=$rows[site]?>" <? if($_POST[houston]==$rows[site]) echo "selected='yes'"; else echo "";?>><? echo $rows[site]?></option> It worked now. Link to comment https://forums.phpfreaks.com/topic/118228-solved-how-to-retain-the-post-value/#findComment-608453 Share on other sites More sharing options...
JasonLewis Posted August 5, 2008 Share Posted August 5, 2008 You can make it a tiny bit smaller by removing the else, as you don't need it because it's just echoing nothing. Link to comment https://forums.phpfreaks.com/topic/118228-solved-how-to-retain-the-post-value/#findComment-608455 Share on other sites More sharing options...
suyesh.amatya Posted August 5, 2008 Author Share Posted August 5, 2008 thanks ProjectFear Link to comment https://forums.phpfreaks.com/topic/118228-solved-how-to-retain-the-post-value/#findComment-608463 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.