I want to retrieve database values using ajax based on date range selected on the form by using codeigniter. The scenario is very straight forward. when the user select the daterange from the datepicker the desired system should callback to DB for retrieveing specific student's report and fill the bootstrap table with retrieved values to new without reloading the page for getting data.
3 Answers
Php query,
select *
from table_name
where date(date) between date('from date') and date('to date')
Ajax for this
$("#generate").on('click', function (e) {
e.preventDefault();
$.ajax({
url: 'path to controller method',
type: 'post',
data: {fromdate:$("#fromdate").val(),todate:$("#todate").val()},
success: function (data) {
alert(data);
}
});
});
For generate button give id as generate, also give from from date and to date
echo the content in controller, it will come in ajax success in data. you can append to required div.
15 Comments
Suman Dahal
hello Nirajan .. i have implemented your suggestion but it seems not worked at all.. i have posted the whole code how i implement .. so please suggest me the right way please
Niranjan N Raju
@SumanDahal which part is not working?? what is alert giving??
Suman Dahal
alert doesn't gives any result at all. button is clicked but it doesn't show any alert
Suman Dahal
i have tested the alert by modifying my controller by just echo 'name'; and it shows the alert but retrieving from DB is not working ...
Suman Dahal
I think the ajax doesn't send the values to controller.. because i have also tested it with echoing the $fromdate , $todate and $studentid ... and it just show me the blank alert.
|
function get_attendance_report(){
$studentid =$_POST['studentid'];
$fromdate = date("Y-m-d", strtotime($_POST['fromdate']));
$todate = date("Y-m-d", strtotime($_POST['todate']));
$this->db->where('date >=', $fromdate);
$this->db->where('date <=', $todate);
$this->db->where('student_id =', $studentid);
$attendance_reports = $this->db->get('attendance')->result_array();
foreach ($attendance_reports as $row){
echo '<tr>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['status'].'</td>';
echo '<td>'.$row['remarks'].'</td>';
echo '</tr>';
}
}
This is how i done it ... Thank You so much @Niranjan
Comments
function get_attendance_report(){
$studentid =$_POST['studentid'];
$fromdate = date("Y-m-d", strtotime($_POST['fromdate']));
$todate = date("Y-m-d", strtotime($_POST['todate']));
$this->db->where('date >=', $fromdate);
$this->db->where('date <=', $todate);
$this->db->where('student_id =', $studentid);
$attendance_reports = $this->db->get('attendance')->result_array();
foreach ($attendance_reports as $row){
echo '<tr>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['status'].'</td>';
echo '<td>'.$row['remarks'].'</td>';
echo '</tr>';
}
}

