wurzel Posted September 20, 2010 Share Posted September 20, 2010 I've got a HTML drop down box as similar to this: <select name="dropdown"> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> If I run a mysql query and get a result of "Option 3", is there anyway using PHP to give Option 3 the selected value? Link to comment https://forums.phpfreaks.com/topic/213936-mysql-querydrop-down-selected/ Share on other sites More sharing options...
Miss_Rebelx Posted September 20, 2010 Share Posted September 20, 2010 Are you asking: If I do a query and it tells me the current value is 'Option 3', then make the 'Option 3' option of the select box the default selected option? You can use javascript to set the default selected option: http://www.w3schools.com/jsref/prop_select_selectedindex.asp And then pass it a variable containing which option ID it should select. Link to comment https://forums.phpfreaks.com/topic/213936-mysql-querydrop-down-selected/#findComment-1113464 Share on other sites More sharing options...
rwwd Posted September 20, 2010 Share Posted September 20, 2010 Not Javascript; AVOID at all costs, not very user friendly if the browser doesn't support it!! Right to have the selected value, extrapolate from this excerpt:- <select name="SomeName" value="<?php echo ((isset($_POST['somename']) && !empty($_POST['SomeName'])) ? $_POST['SomeName'] : '');?>"> <option value="option1">First</option> <option value="option2">Second</option> <option value="option3">Third</option> <option value="option4">Fourth</option> </select> Something along that line anyway, if its set (in the $_POST array) echo it's value in the text area! Typed OTF so more than likely contains errors but hopefully you get the jist... Simple when you think about it, unless I have misunderstood you in which case, ignore everything I just typed ;-p It's late at night here.. Rw Link to comment https://forums.phpfreaks.com/topic/213936-mysql-querydrop-down-selected/#findComment-1113470 Share on other sites More sharing options...
Miss_Rebelx Posted September 21, 2010 Share Posted September 21, 2010 I don't think you can give a SELECT tag a value? I've been googling (for lack of having a book handy) and have yet to find it. Link to comment https://forums.phpfreaks.com/topic/213936-mysql-querydrop-down-selected/#findComment-1113729 Share on other sites More sharing options...
taquitosensei Posted September 21, 2010 Share Posted September 21, 2010 <select name="selectname"> <?php your loop here { $selected=($fieldfromdatabase==$fieldyourcomparingitto)?"selected='seleceted'":"": $html.="<option value='".$idfield."' ".$selected.">".$displayfield."</option>"; } ?> </select> Link to comment https://forums.phpfreaks.com/topic/213936-mysql-querydrop-down-selected/#findComment-1113792 Share on other sites More sharing options...
billckr Posted September 22, 2010 Share Posted September 22, 2010 I've got a HTML drop down box as similar to this: <select name="dropdown"> <option>Option 1</option> <option>Option 2</option> <option>Option 3</option> </select> If I run a mysql query and get a result of "Option 3", is there anyway using PHP to give Option 3 the selected value? Give us the context of what you are trying to do and I think we can give you better help unless taquitosensei's answer is what you needed. Link to comment https://forums.phpfreaks.com/topic/213936-mysql-querydrop-down-selected/#findComment-1113928 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.