dlebowski Posted May 6, 2008 Share Posted May 6, 2008 What i have is a query that returns a list of values in a database for me to select using a drop down box. In this case, I have dates in a database and I want each one of those dates to be provided to me as an option in a drop down. This works perfectly. What I am having trouble with is when I make a selection from the drop down and click "submit" I want the value that was submitted to be the default value in the drop down. Currently, the first date shows up by default because I have "ORDER BY Date" in my query. I want this to actually have the value that I submitted when clicking on "submit" to be the default value in the drop down instead of the earliest date. So if I have $Date=$_POST['Date']; with the Date value being "2008-05-09", I want that date to be the default value in the box. Let me know if this isn't clear and I will elaborate. Thank you in advance. <?php include("dbinfo.inc.php"); mysql_connect("localhost",$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "SELECT * FROM database ORDER BY Date"; $result = mysql_query ($query); echo "<select name=Date>"; while($nt=mysql_fetch_array($result)){//Array or records stored in $nt echo "<option value=$nt[Date]>$nt[Date]</option>"; } echo "</select>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/104437-solved-drop-down-default-value-after-form-submission/ Share on other sites More sharing options...
rhodesa Posted May 6, 2008 Share Posted May 6, 2008 Update this line: echo "<option value={$nt['Date']}".($nt['Date']==$_POST['Date']?' SELECTED':'').">{$nt['Date']}</option>"; Quote Link to comment https://forums.phpfreaks.com/topic/104437-solved-drop-down-default-value-after-form-submission/#findComment-534643 Share on other sites More sharing options...
dlebowski Posted May 6, 2008 Author Share Posted May 6, 2008 That's it! Thank you so much. Quote Link to comment https://forums.phpfreaks.com/topic/104437-solved-drop-down-default-value-after-form-submission/#findComment-534657 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.