Jump to content

Make sure a rating is given via select dropdown


webref.eu

Recommended Posts

Someone on the forum kindly gave me the below code for a Review Rating select dropdown which prints out 1 to 10 in a select box and remembers the rating if the form gets posted back. 

 

I would like to modify this to make sure that the user has actually given a rating.  I am thinking of making the dropdown display an initial value of Please Select, which I have shown under my attempt in the below code.  However, this isn't quite working because 0 is overwriting it when the dropdown is displayed. 

 

Can anyone help me out with the code and also suggest Javascript that could be used to make sure the user has entered a rating, i.e. Please Select is no longer being displayed. 

 

Thanks All.

 

 

<select name="ReviewRating" id="ReviewRating">
  <?php


  //my attempt
  //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 adds the HTML attribute selected to the option tag, if the rating matches the number being printed
  $rating = (isset($ReviewRating)) ? $ReviewRating : '';
  for ($i = 0; $i <= 10; $i++) {
    $selected = ($rating == $i) ? ' selected="selected"' : '';
    echo "<option$selected>$i</option>\n";
  }
  ?>  
  </select>

OK, so I've modified the code and got it working.  Note that the processform flag is set to 1 once the form is being posted back.  So, now the select dropdown will intially display "Please Select".  Can anyone help me out with the Javascript to make sure the user has changed this from "Please Select" to give a rating out of 10. 

 

Thanks All. 

 

<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>

 

Rgds

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.