doubledee Posted April 3, 2011 Share Posted April 3, 2011 Do I need to add value to this select statement? <select id="expYear" name="expYear"> <option></option> <option>2011</option> <option>2012</option> <option>2013</option> <option>2014</option> <option>2015</option> <option>2016</option> <option>2017</option> <option>2018</option> <option>2019</option> <option>2020</option> </select> Debbie Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/ Share on other sites More sharing options...
Zane Posted April 3, 2011 Share Posted April 3, 2011 preferably, unless you're using AJAX. Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196090 Share on other sites More sharing options...
doubledee Posted April 3, 2011 Author Share Posted April 3, 2011 preferably, unless you're using AJAX. And if I left out the value attribute, what is the worst that would happen? Debbie Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196092 Share on other sites More sharing options...
Zane Posted April 3, 2011 Share Posted April 3, 2011 then $_POST['expYear'] or $_GET['expYear'] might possibly have no value. Then again, there is the possibility that it will, you never know until you try. Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196093 Share on other sites More sharing options...
Pikachu2000 Posted April 3, 2011 Share Posted April 3, 2011 Without it, the markup is invalid and there's no guarantee the value will be sent by the browser at all. Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196103 Share on other sites More sharing options...
Zane Posted April 3, 2011 Share Posted April 3, 2011 Without it, the markup is invalid and there's no guarantee the value will be sent by the browser at all. That's true as well, The main thing is what your DOCTYPE is. Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196104 Share on other sites More sharing options...
doubledee Posted April 3, 2011 Author Share Posted April 3, 2011 Without it, the markup is invalid and there's no guarantee the value will be sent by the browser at all. That's true as well, The main thing is what your DOCTYPE is. Gee, I didn't know I made such a big boo boo! Debbie Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196105 Share on other sites More sharing options...
Pikachu2000 Posted April 3, 2011 Share Posted April 3, 2011 Why not build the field dynamically, as long as you're going to edit it anyhow. . . $year = range( 2011, 2020 ); echo "<select id=\"expYear\" name=\"expYear\">\n <option value=\"\"></option>\n"; foreach( $year as $v ) { echo "<option value=\"$v\">$v</option>\n"; } echo '</select>'; Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196108 Share on other sites More sharing options...
doubledee Posted April 3, 2011 Author Share Posted April 3, 2011 Why not build the field dynamically, as long as you're going to edit it anyhow. . . $year = range( 2011, 2020 ); echo "<select id=\"expYear\" name=\"expYear\">\n <option value=\"\"></option>\n"; foreach( $year as $v ) { echo "<option value=\"$v\">$v</option>\n"; } echo '</select>'; Since you suggested turning my static HTML into dynamic PHP... How can I retain the option that the user chose when my form reloads? (e.g. If the user chose "04" under "Month" and my form reloads, then "Month" is blank.) Debbie Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196126 Share on other sites More sharing options...
Pikachu2000 Posted April 3, 2011 Share Posted April 3, 2011 Just check for the value in the $_POST array and compare it to the value for the field. If they match, echo 'selected="selected"'. $year = range( 2011, 2020 ); echo "<select id=\"expYear\" name=\"expYear\">\n <option value=\"\"></option>\n"; foreach( $year as $v ) { $selected = (!empty($_POST['expYear']) && $_POST['expYear'] == $v ) ? 'selected="selected"' : ''; echo "<option value=\"$v\" $selected>$v</option>\n"; } echo '</select>'; Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196231 Share on other sites More sharing options...
doubledee Posted April 3, 2011 Author Share Posted April 3, 2011 Just check for the value in the $_POST array and compare it to the value for the field. If they match, echo 'selected="selected"'. $year = range( 2011, 2020 ); echo "<select id=\"expYear\" name=\"expYear\">\n <option value=\"\"></option>\n"; foreach( $year as $v ) { $selected = (!empty($_POST['expYear']) && $_POST['expYear'] == $v ) ? 'selected="selected"' : ''; echo "<option value=\"$v\" $selected>$v</option>\n"; } echo '</select>'; Awesome!! Just what I needed, and without using JavaScript or Cookies or Sessions! Thanks, Debbie Link to comment https://forums.phpfreaks.com/topic/232533-do-i-need-value-in-my/#findComment-1196274 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.