Nitestarz78 Posted January 9, 2009 Share Posted January 9, 2009 I have a form that autofills in the course name depending on what the user chose. When I reload the page this selection is gone. The page is reloaded with an error message if the user didnt fill out all the necessary fields. How can I get this drop down menu to save it's selection when the page is reloaded? <? $course_array = array( '1', '2', '3', '4', '5' ); print "<select name=Course>"; $i = 0; WHILE ($i < count($course_array)) { if($course_array[$i]==$CourseName) print "<option selected "; else print "<option"; print " value='$course_array[$i]'> $course_array[$i]"; print "</option>"; $i++; } print "</select>"; ?> Please help! Link to comment https://forums.phpfreaks.com/topic/140147-php-variables-and-post/ Share on other sites More sharing options...
gevans Posted January 9, 2009 Share Posted January 9, 2009 if(isset($_POST['Course') && $_POST['Course'] == $course_array[$i]) $selected = "selected=\"selected\""; else $selected = ""; does that make sense? Link to comment https://forums.phpfreaks.com/topic/140147-php-variables-and-post/#findComment-733316 Share on other sites More sharing options...
Nitestarz78 Posted January 9, 2009 Author Share Posted January 9, 2009 I think so....would it replace anything in the current code? Link to comment https://forums.phpfreaks.com/topic/140147-php-variables-and-post/#findComment-733325 Share on other sites More sharing options...
gevans Posted January 9, 2009 Share Posted January 9, 2009 try this <?php $course_array = array( '1', '2', '3', '4', '5' ); print "<select name=Course>"; $i = 0; if(isset($_POST['Course') && $_POST['Course'] == $course_array[$i]) $CourseName = "selected=\"selected\""; else $CourseName = ""; WHILE ($i < count($course_array)) { if($course_array[$i]==$CourseName) print "<option selected "; else{ print "<option"; print " value='$course_array[$i]'> $course_array[$i]"; print "</option>"; $i++; } } print "</select>"; ?> Link to comment https://forums.phpfreaks.com/topic/140147-php-variables-and-post/#findComment-733335 Share on other sites More sharing options...
Nitestarz78 Posted January 9, 2009 Author Share Posted January 9, 2009 It just brings up a blank page now. Is there anyway to error check? Link to comment https://forums.phpfreaks.com/topic/140147-php-variables-and-post/#findComment-733387 Share on other sites More sharing options...
gevans Posted January 9, 2009 Share Posted January 9, 2009 EDIT <?php $course_array = array( '1', '2', '3', '4', '5' ); print "<select name=Course>"; $i = 0; if(isset($_POST['Course']) && $_POST['Course'] == $course_array[$i]) $CourseName = "selected=\"selected\""; else $CourseName = ""; WHILE ($i < count($course_array)) { if($course_array[$i]==$CourseName) print "<option selected "; else{ print "<option"; print " value='$course_array[$i]'> $course_array[$i]"; print "</option>"; $i++; } } print "</select>"; ?> Link to comment https://forums.phpfreaks.com/topic/140147-php-variables-and-post/#findComment-733391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.