webref.eu Posted August 22, 2008 Share Posted August 22, 2008 Hi All I want a dropdown that says "Please Select" in its in initial state, where a user can select a 0 to 10 rating, as per these screen prints: http://www.webref.eu/images/please-select.gif http://www.webref.eu/images/please-select-0-to-10.gif I am letting my form post back on itself. The problem I am having is that if the user forgot to select a rating, I want the form to default back to "Please Select", but currently it is defaulting back to 0. I know this is something to do with how the value from the dropdown (called ReviewRating) is collected and then compared in the code below: Collection of variable on postback: $ReviewRating=$_POST['ReviewRating']; Code for ReviewRating dropdown as it stands, note a $_POST['processform'] value of 1 means the form has been posted back: <select name="ReviewRating" id="ReviewRating"> <?php //If form being shown for the first time If($_POST['processform'] != 1) { echo "<option selected=\"selected\">Please Select</option>\n"; } //loops 1 through 10, and when a postback adds the HTML attribute selected to the option tag, if the rating matches the number being printed for ($i = 0; $i <= 10; $i++) { //If a postback, check which Review Rating was selected If($_POST['processform'] == 1) { $selected = ($ReviewRating == $i) ? ' selected="selected"' : ''; } echo "<option$selected>$i</option>\n"; } ?> </select> So the problem is that if "Please Select" was left in the select dropdown, then on postback this is being interpreted as a 0 in the code that sets the selected value of the dropdown, i.e. this line: $selected = ($ReviewRating == $i) ? ' selected="selected"' : ''; So how can I make it that if "Please Select" was left initially, it still displays on postback? Thanks All. Quote Link to comment Share on other sites More sharing options...
webref.eu Posted August 22, 2008 Author Share Posted August 22, 2008 I guess this is some kind of variable type problem? A string being compared to integer problem? Rgds Quote Link to comment Share on other sites More sharing options...
marcus Posted August 22, 2008 Share Posted August 22, 2008 Set the value of the Please Select to 0. Then change this line If($_POST['processform'] != 1) { to if(!$_POST['processform']) { Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 22, 2008 Share Posted August 22, 2008 one issue i see is that the Please Select option will never even be echoed into the select list if the form has been submitted. give the Please Select option a specific integer value, rather than the "Please Select" it takes simply because it doesn't get a value. this allows you to check against the value directly to see whether it should be selected: if ($_POST['processform'] != 1 || $_POST['ReviewRating'] == 15) { echo "<option value=\"15\" selected=\"selected\">Please Select</option>\n"; } EDIT: @mgall - he wants a 0 option, so it won't work to give the Please Select option a value of 0. Quote Link to comment Share on other sites More sharing options...
webref.eu Posted August 22, 2008 Author Share Posted August 22, 2008 Thanks for the suggestions and in particular akitchin as your suggested method works well. I decided to use a value of 11 instead of 15, here's the modified working code: <select name="ReviewRating" id="ReviewRating"> <?php //If form being shown for the first time //We give Please Select option a value of 11 to make coding for it easier If($_POST['processform'] != 1 || $_POST['ReviewRating'] == 11) { echo "<option value=\"11\" selected=\"selected\">Please Select</option>\n"; } //loops 1 through 10, and when a postback adds the HTML attribute selected to the option tag, if the rating matches the number being printed for ($i = 0; $i <= 10; $i++) { //If a postback, check which Review Rating was selected If($_POST['processform'] == 1) { $selected = ($ReviewRating == $i) ? ' selected="selected"' : ''; } echo "<option$selected>$i</option>\n"; } ?> </select> Thanks again. Rgds 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.