wkilc Posted July 22, 2008 Share Posted July 22, 2008 <form> <select name="trees"> <option value="maple">maple</option> <option value="oak">oak</option> <option value="pine">pine</option> </select> </form> Is there a simple way to use PHP to "hold" (echo selected?) the last value that was selected by the user? Thank you. ~Wayne Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/ Share on other sites More sharing options...
dezkit Posted July 22, 2008 Share Posted July 22, 2008 yes Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-596914 Share on other sites More sharing options...
MasterACE14 Posted July 22, 2008 Share Posted July 22, 2008 <?php if(isset($_GET['selected']) && !empty($_GET['selected'])) { if($_GET['selected'] == 'maple') { $selected = 'selected' } } else { $selected = ''; } ?> <form> <select name="trees"> <option value="maple" <?=$selected?>>maple</option> <option value="oak">oak</option> <option value="pine">pine</option> </select> </form> do that for all 3 of em, using a switch would be better. Regards ACE Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-596918 Share on other sites More sharing options...
trq Posted July 22, 2008 Share Posted July 22, 2008 Assuming your form is posting to itself.... <form method="post" action=""> <select name="trees"> <option value="maple"<?php echo (isset($_POST['trees']) && $_POST['trees'] == 'maple') ? ' selected="selected"' : ''; ?>>maple</option> <option value="oak"<?php echo (isset($_POST['trees']) && $_POST['trees'] == 'oak') ? ' selected="selected"' : ''; ?>>oak</option> <option value="pine"<?php echo (isset($_POST['trees']) && $_POST['trees'] == 'pine') ? ' selected="selected"' : ''; ?>>pine</option> </select> </form> Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-596921 Share on other sites More sharing options...
wkilc Posted July 22, 2008 Author Share Posted July 22, 2008 Thank you. I'm still having trouble, as my form is more complicated than my example. This is what I've got... no errors, but it's not working to hold the value: <select name="link"> <option value="<? echo "$page_name" ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == '$page_name&limit=25') ? ' selected="selected"' : ''; ?>>25</option> <option value="<? echo "$page_name" ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == '$page_name&limit=50') ? ' selected="selected"' : ''; ?>>50</option> </select> Thank you again. ~Wayne Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-596950 Share on other sites More sharing options...
trq Posted July 22, 2008 Share Posted July 22, 2008 Variables are not interpolated within single quotes, so... '$page_name&limit=25' would need to be... "$page_name&limit=25" Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-596958 Share on other sites More sharing options...
trq Posted July 22, 2008 Share Posted July 22, 2008 Oh, and these.... <? echo "$page_name" ?> should be... <?php echo $page_name; ?> Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-596960 Share on other sites More sharing options...
wkilc Posted July 22, 2008 Author Share Posted July 22, 2008 Thank you once again! The code now reads: <select name="link"> <option value="<? echo $page_name ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=25") ? ' selected="selected"' : ''; ?>>25</option> <option value="<? echo $page_name ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=50") ? ' selected="selected"' : ''; ?>>50</option> </select> But the list still defaults to the first value... and when I view the processed source code, there's no evidence of the "selected". Thanks again, ~Wayne Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-596989 Share on other sites More sharing options...
trq Posted July 22, 2008 Share Posted July 22, 2008 Does your form use the post or get methods? Also, you still didn't change a part. <select name="link"> <option value="<?php echo $page_name; ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=25") ? ' selected="selected"' : ''; ?>>25</option> <option value="<?php echo $page_name; ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=50") ? ' selected="selected"' : ''; ?>>50</option> </select> Where is $page_name defined? Why do you even need it there? Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-596992 Share on other sites More sharing options...
wkilc Posted July 22, 2008 Author Share Posted July 22, 2008 $page_name = $_SERVER["REQUEST_URI"]; Post, I think. It grabs the current URL, with all the current queries, and then the form is actually a link that takes that URL and modifies "limit", changing the number of records displayed on a page. It's worked in other instances exactly the way I want it to. Sorry, I was missing the semicolons... I've corrected that, still no dice: <select name="link"> <option value="<?php echo $page_name; ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=25") ? ' selected="selected"' : ''; ?>>25</option> <option value="<?php echo $page_name; ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=50") ? ' selected="selected"' : ''; ?>>50</option> </select> Thanks, ~Wayne Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-597001 Share on other sites More sharing options...
trq Posted July 22, 2008 Share Posted July 22, 2008 Can we see your <form> tag definition? Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-597004 Share on other sites More sharing options...
wkilc Posted July 23, 2008 Author Share Posted July 23, 2008 <form name="form" method="post"> <select name="link"> <option value="<? echo $page_name; ?>&limit=25"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=25") ? ' selected="selected"' : ''; ?>>25</option> <option value="<? echo $page_name; ?>&limit=50"<?php echo (isset($_POST['link']) && $_POST['link'] == "$page_name&limit=50") ? ' selected="selected"' : ''; ?>>50</option> </select> <input type="button" value="Go" onClick="openURL()"> </form> The link is fired by this JS: <script type="text/javascript"><!-- function openURL() { selInd = document.form1.link.selectedIndex; goURL = document.form1.link.options[selInd].value; top.location.href = goURL; } //--> </script> Thank you. ~Wayne Link to comment https://forums.phpfreaks.com/topic/116085-retaingin-pulldown-value/#findComment-597008 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.