我正在研究ajax如何在codeigniter上实际工作。而且,我面临的问题是:在ajaxex.php中,有一个div,即ShowMember',我想在选择'成员ID'之后显示会员详细信息。从下拉列表。数据还不行,但即将发布的内容是' ajaxex.php页面'在' ShowMember'从下拉列表中选择值后的div。请帮忙。我刚开始使用codeigniter。谢谢。
<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>
$('#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);
}
}
<?
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;
}
}
答案 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检测我的错误。这意味着很多。
I have to set Baseurl here in head section of header.
<script>
var BASEURL = 'http://localhost/Project/index.php/';
</script>
没有变化。如问题
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);
}
没有变化,如同问题。
$('#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);
}});
});