根据所选值加载页面后,如何在选择选项中保留所选值

I couldn't keep the selected value after loading the page, that's why I couldn't take the selected value from the select option in Database after done some function.. Here is my code

<select name='courseID' class="mySelect" id='courseID'>
    <?php while ($row1 =mysqli_fetch_array($result1)):;?>
    <option value="<?php echo $row1['CourseID'];?>"><?php echo $row1['CourseID'];?></option><?php endwhile; ?></select>
<script type="text/javascript">document.getElementById('courseID').value="<?php echo $_GET['courseID'];?>";</script>

Did JS can add in a HTML in this way??

You have add condition to check if the course id is in the result.

<?php while ($row1 =mysqli_fetch_array($result1)):;
    $selected = ($_GET['courseID'] == $row1['CourseID']) ? 'selected' : '';
    ?>
        <option value="<?php echo $row1['CourseID'];?>" <?php echo $selected;?>><?php echo $row1['CourseID'];?></option>
    <?php endwhile; ?>