Php 在视图中回显ajax数据

Php 在视图中回显ajax数据,php,jquery,ajax,codeigniter,select,Php,Jquery,Ajax,Codeigniter,Select,我是阿贾克斯的新手,我正在学习。我搜索了一下,但找不到解决办法。我想从我的数据库中选择所有国家,并想在表中回显结果。我得到了结果,但我不知道如何在视野中回应它。谁来帮帮我。提前谢谢 我的控制器Ajax.php function index() { $this->load->view('Ajax/Ajax_view'); } function Get_specific() { $name=$this->input->post('name'); $da

我是阿贾克斯的新手,我正在学习。我搜索了一下,但找不到解决办法。我想从我的数据库中选择所有国家,并想在表中回显结果。我得到了结果,但我不知道如何在视野中回应它。谁来帮帮我。提前谢谢

我的控制器Ajax.php

function index()
{
    $this->load->view('Ajax/Ajax_view');
}
function Get_specific()
{
    $name=$this->input->post('name');
    $data['country']=$this->Ajax_m->select("countries");
    echo json_encode($data);
    $this->load->view('Ajax_view',json_encode($data));
}
我的模型Ajax\u m.php

function select($table){
    $query=$this->db->get($table);

    return $query->result();
}
我的视图Ajax\u View.php

<html>
<head>
                                                                                          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">    
   </script>
 <script type="text/javascript">
$(document).ready(function(){
  $('#data').hide();
    $('#sub').click(function(event){

      event.preventDefault();
      var name=$('#name').val();
      $.ajax({
         type: "POST",
         url : "<?php echo base_url(); ?>" + "index.php/Ajax/Get_specific/",
         datatype : 'html',
         data : { name: name },
         success:function(res){
 $('#data').show();
alert('done')
}      
      });  

  });
});

</script>
</head>
<body>
    <?php echo form_open('Ajax/insert'); ?>
    <input type="text" name="name" id="name" value=""><br>
    <input type="submit" name="sub" id="sub"><br>
    <?php echo form_close(); ?>
    <div id="data">
        <table>
            <tr>
                <th>name</th>
                <th>Delete</th>
            </tr>

            <?php 
            if(isset($query)):
            foreach ($query as $row): ?>
            <tr>
                <td></td>
                <td>Delete</td>
            </tr>
            <?php endforeach;
            endif;?>
        </table>
    </div>
</body>
很难确切地知道您在这里想要什么,但我所做的只是ajax调用的成功,在显示之前向元素添加res


编辑:如果您不想丢失元素中已有的任何内容,可以始终使用.append而不是.html

您可以从控制器返回表格的html页面,然后将其追加为$data.appendres或类似的内容。替换$'data'。show;使用$'data'.htmlres.show;
$.ajax({
     type: "POST",
     url : "<?php echo base_url(); ?>" + "index.php/Ajax/Get_specific/",
     datatype : 'html',
     data : { name: name },
     success:function(res){
    $('#data').html(res).show();
    alert('done')
}