How can i load some database values to a view in codeigneitor through AJAX [closed]

i am struggling with retrieving some database values to view in a codeigniter.i want to load some of data to a first.php which is a view. please give me a way to overcome this situation.

Here is my code

Controller

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

Model

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";
        }

    }

view

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

Answer

See below code

Controller

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

Model

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

    }

view

$.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;

Leave a Reply

Your email address will not be published. Required fields are marked *