完整的页面详细信息来自ajax成功函数结果 - codeigniter

时间:2015-08-31 12:12:47

标签: php ajax codeigniter

我正在研究ajax如何在codeigniter上实际工作。而且,我面临的问题是:在ajaxex.php中,有一个div,即ShowMember',我想在选择'成员ID'之后显示会员详细信息。从下拉列表。数据还不行,但即将发布的内容是' ajaxex.php页面'在' ShowMember'从下拉列表中选择值后的div。请帮忙。我刚开始使用codeigniter。谢谢。

ajaxex.php(查看)

<div>
  <select class='form-control EmployeeIDs' name='EmployeeID' id='EmployeeIdenity'>
                <option value="">Select Employee</option>
                <?
                foreach($results as $row) 
                {
                $MemberID=$row->MemberID;
                $EmployeeID=$row->EmployeeID;
                ?>
                <option value="<?echo $MemberID;?>"><?echo $EmployeeID;?></option>
                <?}?>
            </select>
</div>

<div class='ShowMember'></div>

mystyle.js

$('#EmployeeIdenity').change(function(){
    var MemberID = $('#EmployeeIdenity').val();
    $.ajax({
        url:"welcome/ajaxFindMemberDetails",
        type:"GET",
        data:{MemberID:MemberID},
        success:function(result){
            $('.ShowMember').html(result);
    }});
});

控制器

class Welcome extends CI_Controller
{

    public function __construct()
        {
                parent::__construct();

                $this->load->model('news_model');
        $this->load->library('session'); // Start Session
        $this->load->helper('form');
        $this->load->library('form_validation');

        }
       public function ajaxFindMemberDetails()
       {
        $data1['MemberDetailsResult'] = $this->news_model->memberDetails($MemberID);
        $this->load->view('admin/ajaxMemberDetailsPage',$data1);
       }
}

ajaxMemberDetailsPage.php(VIEW)

<?
foreach($MemberDetailsResult as $rowMemDetails) 
{
    echo $MemID=$rowMemDetails->MemberID;
?>

<?}?>

模型

class News_model extends CI_Model 
{
        public function __construct()
        {
                $this->load->database();
        }
        public function memberDetails($MemberID)
       {
            $this->db->where('MemberID', $MemberID);
            $query = $this->db->get('member');
            $MemberDetailsResult = $query->result();
            return $MemberDetailsResult;
        }
}

2 个答案:

答案 0 :(得分:1)

您在控制器中获得MemberID

public function ajaxFindMemberDetails()
       {
        $MemberID=$this->input->get('MemberID');// get MemberID
        $data1['MemberDetailsResult'] = $this->news_model->memberDetails($MemberID);
        $this->load->view('admin/ajaxMemberDetailsPage',$data1);
       }

视图中的php标记ajaxMemberDetailsPage.php

<?php
foreach ($MemberDetailsResult as $rowMemDetails) {
    echo $MemID = $rowMemDetails->MemberID;

}?>

答案 1 :(得分:1)

实际上,Ajax代码无法找到正确的路径。所以,我试过这个并且它有效。所以,我想分享我的变化。它可能会帮助某人。谢谢@Saty,@ Arnas,@ Indrasinh检测我的错误。这意味着很多。

的header.php

I have to set Baseurl here in head section of header.
<script>
  var BASEURL = 'http://localhost/Project/index.php/';
</script>

AjaxEx.php

没有变化。如问题

<强>控制器

public function ajaxFindMemberDetails()
    {
        $MemberID= ($this->input->get('MemberID') != '' ? $this->input->get('MemberID') : 0);
        $data1['MemberDetailsResult'] = $this->news_model->memberDetails($MemberID);
        $this->load->view('admin/ajaxMemberDetailsPage',$data1);
    }

<强>模型

没有变化,如同问题。

AjaxCode.js

$('#EmployeeIdenity').change(function(){

    var MemberID = $("#EmployeeIdenity option:selected").val();
    $.ajax({
        url:BASEURL+"welcome/ajaxFindMemberDetails",
        type:"GET",
        data:{'MemberID':MemberID,'MemType':'Hello'},
        success:function(result){
            $('.ShowMember').html(result);  
    }});
});