如何在php的同一页面中传递值来查询?

时间:2017-08-21 05:41:42

标签: javascript php html mysql

我有一个下拉框

<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子句中的值...任何帮助..

谢谢,

纳瓦兹

1 个答案:

答案 0 :(得分:0)

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

 <?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>