【问题标题】:how to get value of drop down in same php page with mysql database如何使用mysql数据库在同一php页面中获取下拉值
【发布时间】:2013-10-19 05:27:04
【问题描述】:
<div>
<script type="text/javascript">
$(document).ready(function(){
$('#selectstate').change(function() {
window.location = "dropexmpl.php?stateval=" + $(this).val();
});
$('#selectcity').change(function() {
        window.location = "dropexmpl.php?cityval=" + $(this).val();
    });
});
</div>
</script>
</head>
<body >
<div>
<?php
//state drop down
    echo "<td"." id="."id_sel_state".">";
    $con=mysql_connect("localhost","root","");
        mysql_select_db("data_filter",$con);
    $query=("SELECT id,state_name FROM state_details ");
    $result = mysql_query ($query); 
    echo "<label id='statelab'>Select state : </label>"."<select name='filterstate'id='selectstate' onchange='getstateval()'>";

    while ($nt = mysql_fetch_array($result)){
    echo "<option value='".$nt['id']."'>".$nt['state_name']."</option>";
    }
    echo "</select>";
//city drop down
    $statestrval= $_GET['stateval'];
    echo "<td id='id_sel_city'>";
    $query1="SELECT id,state_id,city_name FROM city_details WHERE state_id=".$statestrval."";                                                                               $result1 = mysql_query ($query1);
    echo "<label>Select city : </label>"."<select name='filtercity' id='selectcity' >";
    while ($nt = mysql_fetch_array($result1)){
    echo "<option value='".$nt['id']."'>".$nt['city_name']."</option>";
    }
    echo "</select>";

//Zone drop down

    $citystrval= $_GET['cityval'];
    echo "<td id="."id_sel_zone".">";
    $query2="SELECT id,city_id,zone_area FROM zone_details WHERE city_id=".$citystrval."";                          

    $result2 = mysql_query ($query2);
    echo "<label>Select industry zone : </label>"."<select name='filterzone' id='selectzone'>";
    while ($nt = mysql_fetch_array($result2)){
    echo "<option value='".$nt['id']."'>".$nt['zone_area']."</option>";
    }
    echo "</select>";
    mysql_close($con);
?>
</div>
</form>
</body>
</html>

在这里,我使用了 3 个下拉状态城市和区域。它可以工作,但是当使用 window.location 代码重新加载页面时,它会向我显示具有刷新效果的页面,它是如何使用 ajax 解决的。 it should be like when i select the state is should be shows city list related to that state.when city is selected it should be shows the zone related to that city .but problem is when i go to select the city it change the state list当我选择区域时,它会因为重新加载页面而更改城市列表,我该如何解决这个问题。

【问题讨论】:

  • 我不明白你的问题...
  • 我如何应用 ajax 代码来获取同一页面中下拉列表的选定值。
  • 贴出的代码中的ajax代码在哪里?
  • $(document).ready(function(){ $('#selectstate').change(function() { window.location = "dropexmpl.php?stateval=" + $(this) .val(); }); $('#selectcity').change(function() { window.location = "dropexmpl.php?cityval=" + $(this).val(); }); });
  • 查看#selectstate 和#selectcity 的更改事件我使用window.location 和#selectstate 和#selectcity 的选定值。那么现在我如何使用ajax在同一页面中传递#selectstate和#selectcity的选定值。

标签: javascript php jquery ajax


【解决方案1】:

我建议你使用 AJAX、jQuery 并避免页面刷新。
查看 jQuery 文档:http://api.jquery.com/jQuery.getJSON/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    相关资源
    最近更新 更多