This question already has an answer here:
Onchange dropdown values query resp. tables - with php , mysql , ajax , jquery
                                
                                    (1个答案)
                                
                        
                                2年前关闭。
            
                    
我有一个下拉框

<select>
<option value=1>A</option>
<option value=2>B</option>
</select>


在PHP代码中我有一个查询

<?php $sql='select * from table where id='.'SELECTOPTIONVALUE' ?>


我需要在where子句(id = SELECTOPTIONVALUE)中传递选定的值
如何从下拉菜单的where子句中传递值...任何帮助..

谢谢,

纳瓦兹

最佳答案

我认为下面的代码可以帮助您解决问题

 <?php
    $sql = 'select * from table where id = '.$_GET['selected'].' ' ;
    ?>
    <select name="selected" id="model">
        <option value="">Select</option>
        <option value="1" <?php if($_GET['selected'] == 1) echo 'selected';?> >A</option>
        <option value="2"  <?php if($_GET['selected'] == 2) echo 'selected';?>>B</option>
    </select>
    <script type="text/javascript">
        var select = document.getElementById("model");
        select.onchange = function(){
            var selectedString = select.options[select.selectedIndex].value;
            window.location = "http://localhost/stack/stack-q/2.php?selected="+selectedString;
        }
    </script>

关于javascript - 如何在php中的同一页中传递值以进行查询? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45789991/

10-09 22:11