Change Selected Option In Select (form) July 02, 2024 Post a Comment I have an intial group of options like these: FirstSeSolution 1: One way:<selectname="item[type]"><option<?=$test==0?'selected="selected"':'';?>value="0"class="dr">First</option><option<?=$test==1?'selected="selected"':'';?>value="1"class="dr">Second</option><option<?=$test==2?'selected="selected"':'';?>value="2"class="dr">Third</option><option<?=$test==3?'selected="selected"':'';?>value="3"class="dr">Fourth</option></select>CopyAnother:<?$selected[$test] = 'selected="selected"'; ?><selectname="item[type]"><option<?=$selected[0];?>value="0"class="dr">First</option><option<?=$selected[1];?>value="1"class="dr">Second</option><option<?=$selected[2];?>value="2"class="dr">Third</option><option<?=$selected[3];?>value="3"class="dr">Fourth</option></select>CopySolution 2: <selectname="item[type]"id="selectBoxId"><optionvalue="0"class="dr">First</option><optionvalue="1"class="dr">Second</option><optionvalue="2"class="dr">Third</option><optionvalue="3"class="dr">Fourth</option></select><scripttype="text/javascript">var test = "<?= $test; ?>"; if (test != '' && parseInt(test)) { document.getElementById('selectBoxId').selectedIndex = test; } </script>CopySolution 3: Remove "[type]" from select name, make it simple to "item". Then execute this code.$test = isset($_POST['item']) ? $_POST['item'] : "0";Solution 4: // assuming you are using a loop: in the loop where you create the options$selected_html = $test == $loop_var ? ' selected="selected" ' : ''; echo"<option value=\"$loop_var\" class=\"dr\"$selected_html>$text</option>"; Copy Share Post a Comment for "Change Selected Option In Select (form)" Top Question Firefox, Chrome, Safari Have Grey Background For Mp4 Html5 Video Any video (that I can make) with a white background becomes… Red Border Still Appears On Inputs After Resetting An HTML 5 Form Firefox I have an HTML 5 form! Great, I'm excited! I decide to … Svg Animation And Firefox I'm struggle with SVG animation drawing, which is work … How To Save Html5 Geolocation Data To Python Django Admin? Is it possible to save the javascript html5 geolocation lat… How To Hide/show Tabs Using Jquery I have four tabs.On loading only the first tab is opened an… Form To Iframe Submission - How Do You Reset The Form After File Upload? I have a form that submits the results to an iframe. The su… How To Limit The Html Table Records In Each Page I am populating a table in my jsp which has more than 20 re… How To Display A Python String As Html In Jupyter Notebook In IPython notebook, I used to be able to display a python … How To Add Placeholder Field? I've been trying to add placeholder in input type='… Bounce Animation On A Scaled Object What is the best way to have an object scale and then perfo…