Javascript database lookups

The place to go for any sort of help with using or developing with Bonfire.
Post Reply
PieOrCake
Posts: 1
Joined: 09 Dec 2017, 23:59

10 Dec 2017, 00:17

G'day everyone.

I discovered Bonfire (0.8.3) a week or so ago while looking for an easy framework to create some simple web apps for my workplace. So far, it seems like just what I need - the module builder is amazing!

I have very little knowledge and experience with web technologies (consider me a noob), so I'm hoping someone here can lend me a hand.

I have a "create" view with a field for Employee ID. As soon as numbers are entered into the field, I'd like to perform a database query for the employee's name and display it on the page before I fill out the other fields and submit. I know that Bonfire has Javascript libraries built-in, but I have no idea how to use them to achieve my goal.

Can someone please point me in the right direction for some AJAX goodness?

Ganu623
Posts: 1
Joined: 08 Aug 2018, 13:15

08 Aug 2018, 13:48

Hey Buddy,
This will be js code:
function get_record()
{
$('#courtid').change(function(){
var ci_csrf_token = "<?php echo $this->security->get_csrf_hash() ?>";
var courtid=$("#courtid option:selected").val();
$('#startdaterow').css('display','block');
if ($('input[name="usertype"]:checked').val() == 'external') {
$('#feedeposit').css('display','block');
}
$('#timeslotdiv').css('display','block');
$("#noofslots").val('');

$("#totalfess").val('');
var dateText=$("#startdate").val();
$.ajax({
type: "post",
url: "index.php/admin/mainmasters/pdsa_ground_booking_form/getsoldtimeslots",
cache: false,
data: {courtid:courtid, ci_csrf_token: ci_csrf_token},

success: function (data) {

var myObj = jQuery.parseJSON(data); console.log(myObj); //alert(myObj);
var soldtimeslotsbycourt = $.map(myObj.timeslots, function(value, index) {
return [value];
});
var instituteslots = $.map(myObj.instituteslots, function(value, index) {
return [value];
});
$("#data").val(data);
defaultdate(dateText,soldtimeslotsbycourt,instituteslots);
//console.log( soldtimeslotsbycourt);
},
error: function () {
alert('Error while request..');
}
});
})
controller function:

public function getsoldtimeslots(){
$courtid=$this->input->post('courtid');
$res=$this->pdsa_ground_booking_form_model->soldtimeslots('',$courtid); //echo $this->db->last_query();exit;
echo json_encode($res); exit;
}

Model Function:

public function soldtimeslots($bookingid='',$courtid='')
{
$this->db->select('time_slot,institute_short_name');
$this->db->from('pdsa_ground_booking_form');
$this->db->join('institute_master', 'pdsa_ground_booking_form.instituteid = institute_master.institute_id','left');

if($bookingid!='')
{
$this->db->where('id !=',$bookingid);
}
if($courtid!='')
{
if(in_array($courtid, array(3,4))){
$this->db->where('courtid ='.$courtid.' or courtid =2');
}else if(in_array($courtid, array(2))){
$this->db->where_in('courtid', array('2','3','4'));
}else{
$this->db->where('courtid =',$courtid);
}
}
$query = $this->db->get();
$result=$query->result();

$timeslots=array();
$instituteslots=array();

foreach($result as $row){

$timeslots2=json_decode($row->time_slot);
if(is_null($timeslots2)){continue;};

foreach($timeslots2 as $timeslot){
$timeslots[]=$timeslot;
$instituteslots[]=$row->institute_short_name;
}
}
$array=array();

$array['timeslots']=$timeslots;
$array['instituteslots']=$instituteslots;

return $array;

}

Post Reply