dqh19413 2017-09-08 23:52
浏览 133
已采纳

如何使用ajax将值传递到另一个页面php

I am trying to pass a value of a select option input using Ajax into another page PHP and get the database and fetch data into select option. However, my problem is there is nothing to show in second select option.

I have the select option called Country which contains Country 1, Country 2, Country 3, Country 4 with cid 1,2,3,4.

what I wanted is past Country ID (cid) into getdata.php and query the database of City which contains City a, city b, city c, etc. and fetch city's data into select option using Ajax.

I need to make city select option dynamically change when I select the country.

here is my database structure :

Country.db

enter image description here

City.db

enter image description here

Here is my script :

dropdown.php

<?php

    require_once "connection.php";
?>

<html>
<head>
    <title>Dropdown ajax</title>
</head>
<body>


<div class="country" >
    <label>Country</label>
    <select name="country" onchange="getId(this.value);">
    <option value=""> -- Select Country -- </option>

    <!-- populate value using php -->

    <?php
        $query = "SELECT * FROM country";
        $result = mysqli_query($con,$query);

        //loop

        foreach ($result as $country) {
        ?>
            <option value="<?php echo $country["cid"]; ?>"> <?php echo $country["country"]; ?> </option>

        <?php 
            } 
        ?>

</select>
</div>
<div class="city">
    <label>City</label>
    <select name="city" id="cityList">

        <option value=""></option>

    </select>
</div>
<script src="jquery-3.2.1.min"></script>
<script type="text/javascript">
    function getId(val){
        // ajax function

        $.ajax({
            type:"POST",
            url:"getdata.php",
            data:"cid="+val,
            success:function(data){
                $(#cityList).html(data);
            }
        });
    }
</script>

</body>
</html>

from the dropdown, I am trying to past cid into getdata.php using ajax and fetch the database into select option inside drop-down.php

getdata.php

<?php

require_once "connection.php";

if(!empty($_POST["cid"])){
    $cid = $_POST["cid"];
    $query = "SELECT * FROM city WHERE cid = $cid";
    $result = mysqli_query($con,$query);

    foreach ($result as $city) {

    ?>
    <option value="<?php echo $city["cityId"];?>"><?php echo $city["city"];?></option>

    <?php       
    }
}

?>

here is my connection.php

<?php
    $con = mysqli_connect("localhost","root","admin","dropdowndb");

    //check connection
    if(mysqli_connect_errno()){
        echo "Failed to connect :".mysqli_connect_errno();
    }
?>

the result is like this :

enter image description here

so how to fix this problem?

  • 写回答

1条回答 默认 最新

  • douyouzheng2209 2017-09-09 01:34
    关注

    The selector inside the ajax success handler must be a string, not identifier:

    success:function(data){
      $('#cityList').html(data);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler