这就是我所做的。我正在使用MVC codeigniter。
$(document).ready(function(){
$.ajax({
type: 'post',
data: $("#attendance-pick").serialize(),
url: 'manageLogbook_controller/date_checker/'+ <?php echo $userid; ?>,
dataType: "json",
success: function(result) {
debugger;
$("#your_attendance").html(result);
}
});
});
结果应该来自。
CONTROLLER
function date_checker($userid){
How can i pass the result to success: function(result) on my View--->
$result['my_attendance'] = $this->ManageLogbook_model->retrieve_attendance($result);
}
MODEL
function retrieve_attendance($userid){
return $this->db->where('userid', $userid)
->get('attendance')
->result();
}
答案 0 :(得分:1)
试试这个:
控制器功能:
$result['my_attendance'] = $this->ManageLogbook_model->retrieve_attendance($result);
echo json_encode($result);
Ajax功能:
success: function(response)
{
var data = JSON.parse(response);
}
并且不要忘记更改ajax url参数:
url: <?php echo base_url().'manageLogbook_controller/date_checker/'.$userid ?>,
答案 1 :(得分:1)
你的ajax
$(document).ready(function(){
$.ajax({
type: 'post',
data: $("#attendance-pick").serialize(),
url: "<?php echo base_url('manageLogbook_controller/date_checker/'.$userid); ?>",
dataType: "json",
success: function(result) {
$("#your_attendance").html(result.my_attendance);
}
});
});
你的控制器
function date_checker($userid){
$my_attendance = $this->ManageLogbook_model->retrieve_attendance($userid);
$this->output->set_content_type('application/json')->set_output(json_encode(array('my_attendance' => $my_attendance));
}
答案 2 :(得分:1)
为什么不试试下面的
$(document).ready(function(){
$.ajax({
type: 'post',
data: $("#attendance-pick").serialize(),
url: 'manageLogbook_controller/date_checker/'+ <?php echo $userid; ?>,
dataType: "json",
success: function(result) {
console.log(result)
//$("#your_attendance").html(result);
}
});
});
控制器
function date_checker($userid){
$id = $this->uri->segment(3);
$value = $this->ManageLogbook_model->retrieve_attendance($id);
echo json_encode($value);
模型
return $this->db->where('userid', $userid)
->get('attendance')
->result();
我在成功部分使用了console.log,以便知道检索哪些数据,以便可以根据需要使用它。如果有错误,请告诉我。所以我可以改进我的答案。
答案 3 :(得分:0)
试试这个..
您视图中的Ajax函数:
$(document).ready(function(){
$.ajax({
type: 'POST',
data: {'user_id':$("#attendance-pick").val()},
url: '<?php echo site_url("manageLogbook_controller/date_checker"); ?>',
success: function(result) {
$("#your_attendance").html(result);
}
});
控制器:
function date_checker(){
$user_id = $_POST['user_id'];
$attendance = '';
$res = $this->ManageLogbook_model->retrieve_attendance($user_id);
foreach($res as $r){
$attendance = $r->attendance;
}
echo $attendance;
}
型号:
function retrieve_attendance($userid){
$this->db->select('attendance');
$this->db->from('your_table_name');
$this->db->where('user_id',$userid);
$res = $this->db->get();
return $res->result();
}
希望这有帮助.. 干杯..
答案 4 :(得分:0)
$(document).ready(function(){
$.ajax({
type: 'post',
data: $("#attendance-pick").serialize(),
url: 'manageLogbook_controller/date_checker/ ?>,
dataType: "json",
success: function(result) {
$("#your_attendance").html(result);
}
});
});
//Controller
function date_checker($userid){
$user_id = $_POST['user_id'];
$value = $this->ManageLogbook_model->retrieve_attendance($user_id);
echo json_encode($value);
}
After this you can parse the json in the success of ajax and display it in the view