Jump to content

[SOLVED] Help with selected item retention in select dropdown


webref.eu

Recommended Posts

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.

 

 

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.