简体   繁体   English

如何保留下拉值作为提交后选择的值

[英]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 我正在做一个Php程序

<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变量。

$_POST variables can only be 'accessed' whenever a POST form is submitted, so when the form is not submitted, $_POST['course'] will be undefined. $_POST变量仅在提交POST表单时才能被“访问”,因此,在不提交表单时, $_POST['course'] POST $_POST['course']将是未定义的。 If you want to use persistant, but also relative variables, use $_GET . 如果要使用持久性,也要使用相对变量,请使用$_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. (如果未设置值,这将导致错误,请确保使用PHP中的if语句为此设置例外),但还需要从URL提取值。 so your url needs to have ?course=<course_value> in it, for example: 因此您的网址中必须包含?course=<course_value> ,例如:

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

Click here for more about POST vs GET requests 单击此处以了解有关POST与GET请求的更多信息

Instead of setting the value with javascript, you should directly write the selected attribute. 而不是使用javascript设置值,您应该直接编写所选属性。

<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. 如果必须在javascript中执行此操作,请确保使用正确的语法。 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 您的示例在行尾有一个错误的" 。如果要将vars输出到javascript中,也应该使用json_encode 。最后一件事-如果您不将其放在document ready事件中,则脚本必须放在select元素之后,您将无法对其进行操作

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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