douhao2011 2016-06-20 08:22
浏览 50
已采纳

Ajax Php依赖下拉选择 - 如何将城市名称而不是id发布到数据库中(从数据提交)?

Few days I try to solve problem but no luck. Problem is next:

  • Web form with dependent dropdown select box where user can select his city, zip and street and that work but.
  • When I submit form in my database no city_name (text) - only city_id (number/value).

So, question is - How to post city_name in database? User is from Rome and I Rome (text) is submited not city_id (number).

Select input:

<div class="form-group">
    <label class="col-md-4 control-label" for="grad">City:*</label>
    <div class="col-md-4">
        <select type="text" name="city_name" id="city_name" class="form-control input-sm">
                    <option value=""></option>
                    <option value="999">---Enter city yourself---</option>
                    <?php $sql_query="SELECT * FROM city_table order by city_id asc";
                          $result=mysqli_query($dbconfig,$sql_query);
                          while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
                    ?>
                    <option value="<?php echo $row['city_id'].'_'.$row['city_name'];?>"><?php echo $row['city_name'];?></option>
                    <?php }?>
                </select>
       </div>
</div>


<!-- Select Basic -->
<div class="form-group">
    <label class="col-md-4 control-label" for="post_no">ZIP code:*</label>
    <div class="col-md-4">
        <select name="zip_name" id="zip_name" class="form-control input-sm" onchange="recalculate_price();">

        </select>
    </div>
</div>


<!-- Select Basic -->
<div class="form-group">
    <label class="col-md-4 control-label" for="address">Street:*</label>
    <div class="col-md-4">
        <select name="street_name" id="street_name" class="form-control input-sm" onchange="recalculate_price();">

        </select>
    </div>
</div>

get_zip_php

<?php
include('db_config.php');
if($_POST['id'])
{
 $id=$_POST['id'];

 $sql_query="SELECT * FROM zip_table where city_id='$id' order by zip_name asc";
 $result=mysqli_query($dbconfig,$sql_query);
 ?>
 <option selected="selected">Select ZIP code :</option><?php
 while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
 {
  ?>
        <option value="<?php echo $row['zip_id']; ?>-<?php echo $row['limousine']; ?>-<?php echo $row['kombi']; ?>-<?php echo $row['mini_van']; ?>"><?php echo $row['zip_name']; ?></option>
        <?php
 }
}
?>

get_street_php

<?php
include('db_config.php');
if($_POST['id'])
{
 $id=$_POST['id'];

 $sql_query="SELECT * FROM street_table where zip_id='$id' order by street_name asc";
 $result=mysqli_query($dbconfig,$sql_query);
 ?>
 <option selected="selected">Select street please :</option><?php
 while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
 {
  ?>
        <option value="<?php echo $row['street_id']; ?>"><?php echo $row['street_name']; ?></option>
        <?php
 }
}
?>

Ajax:

    <script>
$(document).ready(function()
{
$("#city_name").change(function()
     {
      var id=$(this).val();
      var data = 'id='+ id;

      $.ajax
      ({
       type: "POST",
       url: "get_zip.php",
       data: data,
       cache: false,
       success: function(html)
        {
            $("#zip_name").html(html);
            recalculate_price();
        } 
    });
});


$("#zip_name").change(function()
    {
      var id=$(this).val();
      var data = 'id='+ id;

      $.ajax
      ({
       type: "POST",
       url: "get_street.php",
       data: data,
       cache: false,
       success: function(html)
       {
        $("#street_name").html(html);
        recalculate_price();
       } 
       });
    });
});
</script>

If I make some changes on "select city part" (Im beginner and really Im lost in all this) I can`t pass zip.

So, please tell me where to make all changes to pass all that things and post city_name, zip (name...I make it as "xxxx - city") and street name. Please help me, I googled and read here about problems few days and can`t continue work on my project until I fix this. Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dou1908 2016-06-20 09:14
    关注

    Use bellow select tag. It may useful for you

    
    <select type="text" name="city_name" id="city_name" class="form-control input-sm">
        <option value=""></option>
        <option value="999">---Enter city yourself---</option>
        <?php 
            $sql_query="SELECT * FROM city_table order by city_id asc";
            $result=mysqli_query($dbconfig,$sql_query);
            while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
                echo  "<option value='".$row['city_id']."'>".$row['city_name']."</option>";
            }
        ?>
    </select>
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line