weixin_33725126 2018-03-06 12:53 采纳率: 0%
浏览 21

用ajax添加php数据

I have this code:

My mysql table 'countries' contains the columns: id and name

<input type="text" name="country" id="country" class="form-control input-lg" autocomplete="off" placeholder="Type Country Name" />

<input type="hide" name="id_country" id="id_country" />

<script>
$(document).ready(function(){

    $('#country').typeahead({
        source: function(query, result)
        {
            $.ajax({
                url:"fetch.php",
                method:"POST",
                data:{query:query},
                dataType:"json",
                success:function(data)
                {
                    result($.map(data, function(item){
                            return item;
                        }));
                }
            })
        }
    });
});
</script>

<?php
//fetch.php
$request = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
 SELECT * FROM countries WHERE name LIKE '%".$request."%'
";

$result = mysqli_query($connect, $query);

$data = array();

if(mysqli_num_rows($result) > 0)
{
    while($row = mysqli_fetch_assoc($result))
    {
        $data[] = $row["name"];
    }
    echo json_encode($data);
}

?>

The above code is a demo, the full version is here

How can I import the country id and insert it into the id_country input?

Thank you!

  • 写回答

3条回答 默认 最新

  • weixin_33739523 2018-03-06 13:11
    关注

    You can do everything in the same query

    $query = "INSERT INTO table (country_id) SELECT id FROM table";
    

    just change the table value

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试