0

I want if the user clicks submit to return the the selected value when the form reloads

that what I try and no luck

<?php
            $getData = $db->prepare("SELECT * FROM first_university_degree_list");
            if ($getData->execute()) {
                $res = $getData->get_result();
                while ($data = $res->fetch_array()) {
                    ?>
                    <option value="<?php echo $data['univ_degree']; ?>" <?php if($FirstUniversityDegree == $data['univ_degree']) echo 'selected="selected"'; ?>><?php echo $data['univ_degree']; ?></option>
                <?php
                }
            }
            ?>

clearing the question
I have a validation for every field on the form so if any of this fields return false the options on the drop menu will return to the first item I need it to return on the selected item the users selected

2
Contributors
1
Reply
18
Views
3 Years
Discussion Span
Last Post by almostbob
0

the id and name of the select field are missing from the code sample, I have chosen 'fieldname'

<?php if($_POST['fieldname']){ 
echo "<option value='$_POST['fieldname'] selected='selected'> $_POST['fieldname'] </option>";}
else { 
$getData = $db->prepare("SELECT * FROM first_university_degree_list");
 if ($getData->execute()) {
  $res = $getData->get_result();
  while ($data = $res->fetch_array()) {
   echo "<option value='$data['univ_degree']'";   
   if($FirstUniversityDegree == $data['univ_degree']) {echo 'selected="selected"';} 
   echo "$data['univ_degree'] </option>";
  }
 }
} ?>

If the data is posted, use that, else run the select statement

syntax may be incorrect due to not seeing the entire code page, may be _GET etc, but should provide a base for dev

Edited by almostbob

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.