webref.eu Posted August 20, 2008 Share Posted August 20, 2008 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> Link to comment https://forums.phpfreaks.com/topic/120550-make-sure-a-rating-is-given-via-select-dropdown/ Share on other sites More sharing options...
webref.eu Posted August 20, 2008 Author Share Posted August 20, 2008 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 Link to comment https://forums.phpfreaks.com/topic/120550-make-sure-a-rating-is-given-via-select-dropdown/#findComment-621259 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.