jasonstairs Posted June 5, 2007 Share Posted June 5, 2007 Hello, I have the following code in my PHP (form submission) application: <p><b>Field: </b> <select name="field" value="Physics"> <option selected value "<?php echo $field?>"><?php echo $field?></option> <!--<option disabled value="">----------</option>--> <option value ="Biology">Biology</option> <option value ="Chemistry">Chemistry</option> <option value ="Engineering">Engineering</option> <option value ="Et cetera">Et cetera</option> <option value ="Materials Science">Materials Science</option> <option value ="Physics">Physics</option> </select> <b>Discipline: </b> <select name="discipline"> <option selected value = "<?php echo $discipline?>"><?php echo $discipline?></option> <optgroup label="Biology"> <option value ="Cell">Cell</option> <option value ="Micro">Micro</option> <option value ="Molecular">Molecular</option> <option value ="Toxicology">Toxicology</option> <optgroup label="Chemistry"> <option value ="Analytical">Analytical</option> <option value ="Biological">Biological</option> <option value ="Inorganic">Inorganic</option> <option value ="Materials">Materials</option> <option value ="Nuclear">Nuclear</option> <option value ="Organic">Organic</option> <option value ="Physical">Physical</option> </optgroup> <optgroup label="Engineering"> <option value ="Chemical">Chemical</option> <option value ="Mechanical">Mechanical</option> </optgroup> <optgroup label="Et cetera"> </optgroup> <optgroup label="Materials Science"> <option value ="Materials">Materials</option> </optgroup> <optgroup label="Physics"> <option value ="Astronomy">Astronomy</option> <option value ="Atomic">Atomic</option> <option value ="Materials">Materials</option> <option value ="Nuclear">Nuclear</option> <option value ="Particle">Particle</option> <option value ="Weapons">Weapons</option> </optgroup> </select> (required)</p><br /> After a user submits the form (of which the code above is just a part) the data goes to a database and is retrieved automatically to fill out the form each time the user visits the page in order for them to know what info they had uploaded. In the section of code above, I want the user to select from drop down menus, their field and discipline and as I mentioned, have the drop down menus select the items relevant to each user each time the user re-visits the page. I used the following code to do so (as shown in the larger chunk above): <option selected value "<?php echo $field?>"><?php echo $field?></option> This works fine in IE but when users in Firefox go to the page, there is a problem.... In both browsers the proper and relevant items (selected by the users in the past) are listed in the top of the drop down menu (per the above code) and are therefore visible; however, in Firefox, if a user tries to resubmit their data (for an edit, etc) the drop down menus return an error as if none of the items of the drop down boxes were selected. That is, even though the drop down menu lists the correct data and shows that it is selected, in Firefox this appears to be untrue as far as the PHP code is concerned. If the user goes and re-selects the data in the drop down menu before re-submitting their data, there is no problem. Is there anyone that can help me determine what changes I can make to get the program to work in both IE and Firefox? Quote Link to comment https://forums.phpfreaks.com/topic/54347-problem-with-html-form/ Share on other sites More sharing options...
leap500 Posted June 5, 2007 Share Posted June 5, 2007 Hi I think you may have missed an = sign after 'value', you also may want to use selected="selected" try: <option selected="selected" value="<?php echo $field?>"><?php echo $field?></option> Quote Link to comment https://forums.phpfreaks.com/topic/54347-problem-with-html-form/#findComment-268814 Share on other sites More sharing options...
Barand Posted June 5, 2007 Share Posted June 5, 2007 I prefer to use a function <?php function fieldSelect($field) { $fields = array('Biology','Chemistry','Engineering','Et cetera','Materials Science','Physics'); $str = "<select name='field'>\n"; foreach ($fields as $f) { $sel = $f==$field ? 'selected' : ''; $str .= "<option value='$f' $sel> $f</option>\n"; } $str .= "</select>\n"; return $str; } /** * call the function passing the current value */ $currentfield = 'Physics'; echo fieldSelect($currentfield); ?> Quote Link to comment https://forums.phpfreaks.com/topic/54347-problem-with-html-form/#findComment-268821 Share on other sites More sharing options...
Psycho Posted June 5, 2007 Share Posted June 5, 2007 Agree with Barand - duplicating the value at the top of the select list is not standard behavior and could cause confusion. However, since you have several select lists I would recommend making the function more generic so you can pass the select list values and the selected value: <?php function fieldSelect($fieldList, $selected) { foreach ($fieldList as $option) { $str .= "<option value='$f'". (($selected==$option)?' selected':'') . "> $f</option>\n"; } return $str; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54347-problem-with-html-form/#findComment-268830 Share on other sites More sharing options...
jasonstairs Posted June 5, 2007 Author Share Posted June 5, 2007 Ahhhh, it was simply the missed equals sign (leap500). Thank you for adding the extra eyes to my problem as well as the great function options (Barand/mjdamato)! I am going to give them a try! Quote Link to comment https://forums.phpfreaks.com/topic/54347-problem-with-html-form/#findComment-268836 Share on other sites More sharing options...
Barand Posted June 5, 2007 Share Posted June 5, 2007 ~mj I don't think the generic verion will work for the next dropdown - it's going to need a 2D array Quote Link to comment https://forums.phpfreaks.com/topic/54347-problem-with-html-form/#findComment-268838 Share on other sites More sharing options...
jasonstairs Posted June 5, 2007 Author Share Posted June 5, 2007 Barand and MJ? How can I make a second function to cover the next drop down list? A 2D array? I would like to, if possible and I assume it is (I am new at PHP but learning that it can do anything), make the second drop down dependent on the first. That is, if a person selects chemistry, then the second drop down will only show the chemistry options. Thank you for all of your help!@ I already learned a lot from this post. Jason Quote Link to comment https://forums.phpfreaks.com/topic/54347-problem-with-html-form/#findComment-268859 Share on other sites More sharing options...
Psycho Posted June 6, 2007 Share Posted June 6, 2007 @Barand, yep I missed that one. But, I still stand by making functions as generic as possible to maximize portability and reusability. The code below contains a working page with a revised function that can support both types of lists. <?php function fieldSelect($fieldList, $selected) { foreach ($fieldList as $optionKey => $option) { if (is_array($option)) { $str .= "<optgroup label=\"$optionKey\">\n"; foreach ($option as $optionValue) { $str .= "<option value=\"$optionValue\"". (($selected==$optionValue)?' selected':'') . ">$optionValue</option>\n"; } $str .= "</optgroup>\n"; } else { $str .= "<option value=\"$option\"". (($selected==$option)?' selected':'') . ">$option</option>\n"; } } return $str; } $oneDimensionSelected = "Engineering"; //Get from database or form submission $twoDimensionSelected = "Nuclear"; //Get from database or form submission ?> <html> <head> <body> One Dimensional Select: <select name="onedimension"> <?php $optionArray = array('Biology','Chemistry','Engineering','Et cetera','Materials Science','Physics'); echo fieldSelect($optionArray, $oneDimensionSelected); ?> </select> <br><br> Two Dimensional Select: <select name="twdimension"> <?php $optionArray = array( "Biology" => array ("Cell", "Micro", "Molecular", "Toxicology"), "Chemistry" => array ("Analytical", "Biological", "Inorganic", "Materials", "Nuclear", "Organic", "Physical") ); echo fieldSelect($optionArray, $twoDimensionSelected); ?> </select> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/54347-problem-with-html-form/#findComment-268876 Share on other sites More sharing options...
jasonstairs Posted June 6, 2007 Author Share Posted June 6, 2007 Thanks! That worked great!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/54347-problem-with-html-form/#findComment-268963 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.