如何通过AJAX将一些数据库值加载到codeigneitor中的视图中

时间:2016-06-10 03:12:27

标签: php jquery ajax codeigniter

我正在努力检索一些数据库值以在codeigniter中查看。我想将一些数据加载到first.php这是一个视图。请给我一个克服这种情况的方法。

这是我的代码

控制器

public function getuserdeatils()
    {
        $this->load->model("user_mod");
        $result = $this->user_mod->getuser($this->input->post("uniquekey"));
    }

模型

public function getuser($uniquekey)
    {
        $q = mysql_query("SELECT fname, Mnumber, address, title, sitename, descr FROM users WHERE uniquekey = '$uniquekey'");
        $yyy = mysql_fetch_row($q);

        if(mysql_num_rows($q) > 0)
        {
            echo "ok";
        }
        else
        {
            echo "wrong";
        }

    }

视图

$.ajax({
                        type: "POST",
                        url: "<?php echo site_url('form_con/getuserdetails'); ?>",
                        data: "fname="+ fname, "Mnumber="+ Mnumber ,
                        success: function(html){
                            $("#disp").html(html);
                        }
                    });
                    return false;

2 个答案:

答案 0 :(得分:2)

见下面的代码

<强>控制器

public function getuserdeatils()
{
     $this->load->model("user_mod");
     $result = $this->user_mod->getuser($this->input->post("uniquekey"));
     echo json_encode(['data'=>$result]);
}

<强>模型

public function getuser($uniquekey)
{
        $q = mysql_query("SELECT fname, Mnumber, address, title, sitename, descr FROM users WHERE uniquekey = '$uniquekey'");

        if($q->num_rows() > 0)
        {
            return $q->row_object();
        }
        else
        {
            return FALSE;
        }

    }

查看

$.ajax({
          type: "POST",
          url: "<?php echo site_url('form_con/getuserdetails'); ?>",
          data: {fname: fname, Mnumber: Mnumber} ,
          success: function(html){
               var res = JSON.parse(html);
               var fname = res.data.fname;
               var Mnumber = res.data.Mnumber;
                // similarly for other variables
                // do what ever you want to do with your variables and append it.
          }
 });
                    return false;

答案 1 :(得分:1)

的Controler

   public function getuserdeatils()
   {
    $this->input->get('uniquekey');
    $this->load->model("user_mod");
    $result = $this->user_mod->getuser($this->input->post("uniquekey"));
    json_encode($result);
    echo $result;
   }

模型

    public function getuser($uniquekey)
    {
        $result = $this->db->select('fname, Mnumber, address, title, sitename, descr')
           ->from('users')
           ->where('uniquekey',$uniquekey)
           ->get()->result();;
       if ($result->num_rows() > 0)
       {
           return $result;
       } else {

           $result = 0;
           return $result;
       }
    }

的Ajax

     $.ajax({
           type: "GET",
           url: "<?php echo site_url('form_con/getuserdetails'); ?>Mnumber="+ Mnumber,
           success: function(html){
               $("#disp").html(html);
           }
     });

未经测试。但这会给你一个想法