简体   繁体   English

如何在不提交的情况下将选定的下拉值存储到php变量?

[英]How to store a selected dropdown value to a php variable without submitting?

I have dropdown. 我有下拉菜单。 When I select any value and click on confirm, Below it should print the selected value and also the selected value should be shown in the dropdown. 当我选择任何值并单击确认时,它下面应打印所选值,并且所选值也应显示在下拉列表中。 How can I achieve this. 我该如何实现。

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <!--Links related to dropdown-->
    <link href="http://localhost/performance/Test/css/fselect.css" rel="stylesheet">
    <script src="http://localhost/performance/Test/js/fSelect.js"></script>

     <script>
    (function($) {
        $(function() { 
            $('#project_name').fSelect();
        });
    })(jQuery);
    </script>
</head>

<body>
    <?php
        $projects = array('Trial','Test','Birds','Animals');
    ?>

    <div class="container">
        <div><center><b>This test will be created in the selected project:</b></center></div><br>
        <div class="create_test">
            <form action="#" method="post">
                <div style="margin-left:2cm;">
                    <select  name="project_name" id="project_name" >
                        <?php
                            for($i=0; $i<count($projects); $i++) {
                                echo "<option value='".$projects[$i]."'>".$projects[$i]."</option>";    
                            }
                        ?>
                    </select>
                    <input type="submit" style="margin-left:3.5cm; width:140px;" name="submit"  id="comfirm" class="btn btn-success" value="Confirm" />
                </div>
            </form>
        </div>
    </div>
</body>
</html>
<?php 
/** Store selected in a varaible in php */
$selected = 'trial';// Suppose get the value from db here
                            for($i=0; $i<count($projects); $i++) {
                                echo "<option value='".$projects[$i]."' selected='".$project[$i] == $selected."'>".$projects[$i]."</option>";    
                            }
                        ?>

So adding the selected='true' will be used as value for the select tag 因此,添加selected ='true'将用作select标签的值

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

相关问题 如何在不提交的情况下不断存储输入值 - How to constantly store input value without submitting 使用选定的下拉框值作为php中的变量 - use selected dropdown box value as variable in php 如何将选定的值存储在变量中? - How to store the selected value in a variable? 如何在div中存储下拉选择的值 - How to store dropdown selected value in a div 从下拉列表中将选定的值分配给PHP变量onchange,而无需按html表单中的“提交”按钮? - Assigning a selected value from a dropdown list to a php variable onchange, without pressing submit button in a html form? 如何在页面加载时在Javascript变量和php值之间制作php IF语句而不提交表单 - How to make php IF statement between Javascript variable and php value on page load without submitting form 如何通过单击按钮(不提交表单)将Textbox值获取到同一php文件中的php变量? - How to get Textbox value to a php variable within same php file with a button click (without submitting the form)? 如果选择了值,则PHP下拉列表 - PHP dropdown if value is selected 如何在不提交表单的情况下将js变量赋值给php变量 - how to assign js variable to php variable without submitting the form 如何在不使用php提交数据的情况下传递下拉列表中的选定值? - How to pass selected value in dropdown list without submit data using php?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM