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

    评论

报告相同问题?