unemployment Posted October 29, 2010 Share Posted October 29, 2010 How can I maintain selected='selected' when the page is reloaded? I don't want to just echo out what they have selected, but rather maintain which option has been selected. Here is my drop down. <form method='POST' id='jumpbox' action='./entrepreneur.php' onsubmit='if(document.jumpbox.industry.value == -1){return false;}'> <fieldset class='jumpbox'> <select name='industry' id='industry' onchange='if(this.options[this.selectedIndex].value != -1){ document.forms['jumpbox'].submit() }'> <option value='-1' selected='selected'>Select an Industry</option> <option value='-1'>------------------</option> <option value='1'>Digital Media</option> <option value='2'>Software</option> <option value='3'>Telecommunication</option> </select> <input type='submit' value='Go' class='go'/> </fieldset> </form> Quote Link to comment Share on other sites More sharing options...
johnrb87 Posted October 29, 2010 Share Posted October 29, 2010 I think you would do something like <option value='2' <?php if($_POST['industry'] == 2) { ?>selected="selected"<?php } ?>>Software</option> <option value='3' <?php if($_POST['industry'] == 3) { ?>selected="selected"<?php } ?>>Telecommunication</option> Quote Link to comment Share on other sites More sharing options...
rwwd Posted October 29, 2010 Share Posted October 29, 2010 Or the same thing done a little more elegantly:- <option value='2' <?php echo (isset($_POST['industry']) && ($_POST['industry'] == 2) ? 'selected="selected"' : '' );?>>Software</option> Etc.. But I advise that you use isset() to check that the $_POST value is actually there before doing anything, else this could result in an undefined index error. Just thought I should point that out. Also when doing this, check which version of html you are doing as html is 'selected' and xhtml is selected="selected", though i may have those two mixed up, but you get the gist - this only applies if you are bothered about passing validation, which IMO, is what every good 'grammer should aspire too. Rw Quote Link to comment Share on other sites More sharing options...
unemployment Posted October 29, 2010 Author Share Posted October 29, 2010 Can someone please explain why I need to use POST? Why do I need to post my selection to the database? Do I need to store the value they have selected and if I do, do I just need to make a separate database field called industry? Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted October 29, 2010 Share Posted October 29, 2010 Review this: http://www.google.com/#sclient=psy&hl=en&q=php+state+saving&aq=f&aqi=g2g-o1&aql=&oq=&gs_rfai=&pbx=1&fp=ddfbf15c2e2f4021 PHP cannot maintain data integrity between pages, without some form of state saving. Even something so simple as pre-populating or re-populating form fields requires some form of data saving/communication. Whether you send it via $_GET, $_POST, $_REQUEST (a combination of both), $_COOKIE, $_SESSION, flat file, or database. The simplest out of thees options for what you are trying to do, is simply perform what was suggested. Re-acquire the $_POST responses on submission and use those to re-populate the form appropriately. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 29, 2010 Share Posted October 29, 2010 How can I maintain selected='selected' when the page is reloaded? Can someone please explain why I need to use POST? Why do I need to post my selection to the database? Do I need to store the value they have selected and if I do, do I just need to make a separate database field called industry? You need to elaborate on what you mean by "when the page is reloaded". If you mean reload as in the user clicks the refresh button, that becomes an elaborate solution as it would require AJAX code to fire wheneveer a change is made to the select list. I think the people above assumed - as did I - that you meant if the user submits the form you wanted to redisplay the field with the submitted value pre-selected. If so, that data is sent within the $_POST variable since that is the method you specified in the FORM tag. This has nothing to do with storing the values in the database (although you are free to do that if you wish). Your code would suggest that changing the select option is submitting the form based on the onchange trigger. As for preselecting the value, here is a more elegant solution than the ones posted previously: <?php $options = array( -1 => 'Select an Industry', 0 => '------------------', 1 => 'Digital Media', 2 => 'Software', 3 => 'Telecommunication', ); $selected = (isset($_POST['industry'])) ? $_POST['industry'] : fasle; $options = ''; foreach($options as $value => $label) { $options .= "<option value=\"{$value}\">{$lable}</option>\n"; } ?> <select name="industry" id="industry" onchange="if(this.options[this.selectedIndex].value>0){ this.form.submit(); }"> <?php echo $options; ?> </select> Quote Link to comment Share on other sites More sharing options...
unemployment Posted October 29, 2010 Author Share Posted October 29, 2010 How can I maintain selected='selected' when the page is reloaded? Can someone please explain why I need to use POST? Why do I need to post my selection to the database? Do I need to store the value they have selected and if I do, do I just need to make a separate database field called industry? You need to elaborate on what you mean by "when the page is reloaded". If you mean reload as in the user clicks the refresh button, that becomes an elaborate solution as it would require AJAX code to fire wheneveer a change is made to the select list. I think the people above assumed - as did I - that you meant if the user submits the form you wanted to redisplay the field with the submitted value pre-selected. If so, that data is sent within the $_POST variable since that is the method you specified in the FORM tag. This has nothing to do with storing the values in the database (although you are free to do that if you wish). Your code would suggest that changing the select option is submitting the form based on the onchange trigger. As for preselecting the value, here is a more elegant solution than the ones posted previously: <?php $options = array( -1 => 'Select an Industry', 0 => '------------------', 1 => 'Digital Media', 2 => 'Software', 3 => 'Telecommunication', ); $selected = (isset($_POST['industry'])) ? $_POST['industry'] : fasle; $options = ''; foreach($options as $value => $label) { $options .= "<option value=\"{$value}\">{$lable}</option>\n"; } ?> <select name="industry" id="industry" onchange="if(this.options[this.selectedIndex].value>0){ this.form.submit(); }"> <?php echo $options; ?> </select> Am I forgetting something? This code probably works awesome, but I get this error message... Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 29, 2010 Share Posted October 29, 2010 Am I forgetting something? This code probably works awesome, but I get this error message... Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING You must not have read my signature which states I don't always test my code. I expect users to at least be able to identify and fix simple syntax errors. I suspect the error you are receiving is due to the fact that I left a comma after the last array element. But, I'm not guaranteeing there are not others. Quote Link to comment 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.