简体   繁体   中英

how to retain drop down values as the value selected after submit

My drop down is showing blank then when i select the value of dropdown the same value is showing, but i have to show dropdown value as select first then when I click on button the respective value should show

I am doing a Php program

<form class="form-horizontal" name="form" method="post" action="<?php $_PHP_SELF?>">
                                                    <label for="courseDisp" class="col-sm-2" style="margin-top:10px;">Course : </label>

                                                            <?php
                                                            $course="SELECT * from course";
                                                            $res= $conn->query($course);
                                                            if($res->num_rows>0)
                                                            {
                                                                echo '<select name="courseDisp" id="courseDisp" class="form-control col-sm-3" style="margin-top:8px;display:inline;padding:10px;">';
                                                                echo '<option value="0" selected> -- SELECT --</option>'; 
                                                                while($row=$res->fetch_assoc())
                                                                {
                                                                echo '<option value='.$row["course_id"].'>'.$row['shortname'].'</option>';
                                                                }
                                                                echo '</select>';
                                                            } else {
                                                            echo "0 result";
                                                        }

                                                            ?>

                                                    &nbsp;&nbsp;

                                                    <label for="yearDisp" class="col-sm-2" style="margin-top:10px;">Year : </label>


                                                    <?php
                                                            $year="SELECT distinct(year) from syllabus";
                                                            $res= $conn->query($year);
                                                            if($res->num_rows>0)
                                                            {
                                                                echo '<select name="yearDisp" id="yearDisp" class="form-control col-sm-3" style="margin-top:8px;display:inline;padding:10px;">';
                                                                echo '<option value="0">-- SELECT --</option>';
                                                                while($row=$res->fetch_assoc())
                                                                {
                                                                echo '<option value='.$row["year"].'>'.$row['year'].'</option>';
                                                                }
                                                                echo '</select>';
                                                            } else {
                                                            echo "0 result";
                                                        }


                                                            ?>
<script type="text/javascript"> 

document.getElementById('courseDisp').value = "<?php echo $_POST['courseDisp'];?>";     
document.getElementById('yearDisp').value = "<?php echo $_POST['yearDisp'];?>"; 

                                                    &nbsp;

                                                    <input type="submit" class="btn col-sm-2" style="margin-left:15px;margin-top:10px;width:60px;font-weight:bold;font-size:15px;" value="GO" name="btnGo" id="btnGo" />

                                                </form>

I think you are doing it in a wrong way: your code should look like this

 <script type="text/JavaScript">
    var valueSelected=document.getElementById('course').value;
   alert(valueSelected);// do here according to the need
 </script>

This is because there is no $_POST variables present before you submit a form.

$_POST variables can only be 'accessed' whenever a POST form is submitted, so when the form is not submitted, $_POST['course'] will be undefined. If you want to use persistant, but also relative variables, use $_GET .

This can be done the following way:

<script type="text/javascript">
    document.getElementById('course').value =<?php echo $_GET['course'];?>";
</script>

(this will cause an error if value is not set, make sure to make exceptions for that, using if statements in PHP) but the value also needs to be fetched from the URL. so your url needs to have ?course=<course_value> in it, for example:

https://example.com/index.php?course=Course%201

Click here for more about POST vs GET requests

Instead of setting the value with javascript, you should directly write the selected attribute.

<select name="course">
<?php foreach ($options as $key => $value): ?>
    <option value="<?= $key ?>"<?php if ($key == $_POST['course']) echo " selected" ?>>
        <?= $value ?>
    </option>
<?php endforeach; ?>
</select>

If you have to do this in javascript, keep sure, you use the correct syntax. Your example has a wrong " at the end of the line. Also you should use json_encode , if you want to output vars into javascript. And a last thing - if you don't put this inside the document ready event, the script has to be placed after the select element, which you wan't to manipulate

<select name="course">...</select>
...
<script type="text/javascript">
    document.getElementById('course').value = <?= echo json_encode($_POST['course']) ?>;
</script>

需要保留<option value="">-Select-</option>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM