Full Calendar and CodeIgniter send Start and End date - php

I'm trying to get records by ajax request. I manage to get all the records from the table but now I want to send start and end date to controller to get only records from that interval.
What I have so far :
Script:
events: {
url: 'http://localhost/cms/calendar/get_calendar_ajax',
type: 'POST',
cache: true,
},
Controller:
public function get_calendar_ajax() {
$response = json_encode($this->Calendar_model->getRecords() );
echo $response;
}
Model:
public function getRecords() {
$data = $this->db->select()
->from('fullcalendar')
->get()->result();
return $data;
}
I have tried :
Controller :
$response = json_encode($this->Calendar_model->getRecords($_GET['start'], $_GET['end']) );
Model :
public function getRecords($start, $end) {
$data = $this->db->select()
->from('fullcalendar')
->where('start >=', $start)
->where('end <=', $end)
->get()->result();
return $data;
}
but with no luck ..

I manage to find the error:
from:
type: 'POST',
change to
type: 'GET',

Related

use ajax call to load a view codeigniter

I am trying to load a view through ajax passing in a post variable through controller following is my ajax call:
$('#image_id').on('click',function(){
var image_id = $('#image_id').val();
alert(image_id);
if(image_id != '')
{
alert('called');
$.ajax({
url:"<?php echo site_url('Site/load_add')?>",
method:"POST",
dataType: "html",
data:{add_id:image_id},
success:function(data)
{
window.location.href = "<?php echo site_url('Site/load_add')?>";
}
});
}
else
{
alert('Error');
}
});
I am using the add_id to retrieve data from the database in my controller below:
public function load_add()
{
$this->load->model('Product_model');
$data['advert_data']=$this->Product_model->get_specific_add($this->input->post('add_id'));
$array_info2 = array(
"title" => "View Add"
);
$this->load->view('include/header', $array_info2);
$this->load->view('include/navbar_logged_in');
$this->load->view('Site/site_load_add',$data);
$this->load->view('include/footer');
}
I am getting an error at the point of data retrieval from DB, as the model does not have the add_id. Following is my model code
public function get_specific_add($image_id)
{
$this->db->select('adverts.title,adverts.item_condition,adverts.add_type,adverts.address,adverts.price,adverts.negotiable,adverts.product_description,adverts.create_time, users.first_name,users.last_name,users.mobile,users.email,users.image_name');
$this->db->from('adverts,countries,states,cities,users');
$this->db->where('adverts.id= $image_id_new');
$this->db->where('adverts.country_id=countries.id');
$this->db->where('adverts.city_id=states.id');
$this->db->where('adverts.area=cities.id');
$this->db->where('adverts.user_id=users.id ');
$this->db->where('adverts.id= $image_id');
$query = $this->db->get();
return $query->result();
}
Appreciate if someone can help me on this. The page i am trying to load is Site/site_load_add. Thanks
You've call same url with both GET|POST, First call POST is ok, but in GET that's your error.
$.ajax({
// first call is OK!
url:"<?php echo site_url('Site/load_add')?>",
method:"POST",
dataType: "html",
data:{add_id:image_id},
success:function(data)
{
// BUG HERE, This's a GET call and with No query string
// Will `$this->input->post('add_id')` found nothing.
window.location.href = "<?php echo site_url('Site/load_add')?>";
}
Thank you all for the support, I got this sorted by passing the ID of the advert a below:
public function load_add($id)
{
$this->load->model('Product_model');
$data['advert_data'] = $this->Product_model->get_specific_add($id);
$data['adverts'] = $this->Product_model->get_adverts();//get database query output to the array
$array_info2 = array(
"title" => "View Add"
);
$this->load->view('include/header', $array_info2);
$this->load->view('include/navbar_logged_in');
$this->load->view('Site/site_load_add', $data);
$this->load->view('include/footer');
}
Thanks

Getting 500 error code while fetching data from DB using Ajax in Laravel

I am getting a 500 error code while fetching data from the database using Ajax in Laravel with the following function:
public function search(Request $request)
{
if ($request->ajax()) {
$query = $request->get('query');
$data = Constant_model::get_icons('fontawesomeicons', 'id', 'DESC', 20, $query);
}
return response($data->jsonSerialize(), Response::HTTP_OK);
}
get_icons function is:
public static function get_icons($table, $order_column, $order_type, $limit, $search = '')
{
$result = DB::table($table)
->select('*')
->orderBy($order_column, $order_type)
->where('icon_name', 'like', '%'.$search.'%')
->paginate($limit);
return $result;
}
I am trying to get the data using Ajax with the following Javascript:
$.ajax({
url: "/icons/search",
method: "GET",
data: {
search: query
},
dataType: 'json',
error: function (error) {
console.log(error);
},
success: function(response) {
alert(response);
}
});
Class App\Http\Controllers\Response. You forgot to use Response in above your code.
Put that in:
use Response;
Or just use a helper, if you don't want to import it:
return response()->json(...);
Now in your case, you must return it, like this:
return response()->json($data->jsonSerialize(), Response::HTTP_OK);

How to retrieve a parameter from ajax to laravel API Controller

I have an API that requires a string parameter. I want to take the query parameter to the controller and process there. I tried $ajax_data = Input::get('query'); but it didnt work. Searched the same question but cant find a decent answer. Current error is $ajax_data is empty.
My ajax request:
const sendAPIRequest = function (csrf, f) {
$.ajax({
async: false,
url: 'api/apitest',
method: 'get',
data:{
query:"select?facet=on&q=*:*&rows=1&json.facet={Categories:{type:terms,field:price,limit:3}}"
},
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer ' + tkid);
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('X-CSRF-TOKEN', csrf.trim());
},
success: function (data) {
f(data);
},
error: function(xhr) {
//Do Something to handle error
}
});
};
My Controller API part:
public function apitest(){
$ajax_data = Input::get('query');
$user_type = UserAuthorizationHelper::user_authorization();
if ($user_type == Authorities::EDIT_ORGANISATIONS) {}
$query = new GuzzleHttp\Client();
try {
$response = $query->request('GET',SolrController::$url.$ajax_data);
} catch (GuzzleHttp\Exception\GuzzleException $e) {}
$data = $response->getBody()->getContents();
return response()->json(json_decode($data));
}
You are having a problem in this line:
$ajax_data = Input::get('query');
When you are making a request, Request object is sent with the data.
So, instead of Input replace it with Request's object, and you will get the desired output.
Something like this:
// Don't forget to import the Request's namespace
public function apitest(Request $request)
{
$ajax_data = $request->get('query');
// ... Rest of your code
}

i am getting error as trying to get property of non object

i am getting sql result as array..Then it show me an error like `
trying to get property of non-object in controller in
`$rate=$product_det->sale_rate;
This sale_rate is a field in my table product.I am new to PHP. Can anyone help?
My model:
function get_productDet($item)
{
$this->db->select('*');
$this->db->from('product');
$this->db->where('id',$item);
$res=$this->db->get()->result_array();
return $res;
}
My controller:
function product_det()
{
$item=$this->input->post('item_id');
$quantity=$this->input->post('quantity');
$rate=$this->input->post('rate');
$amount="";
$cgst="";
$sgst="";
$igst="";
$product_det=$this->sale_model->get_productDet($item);
if(!empty($product_det))
{
if($rate=="" || $rate==0)
{
$rate=$product_det->sale_rate;
}
$amount=$rate*$quantity;
$cgst_per=$product_det->CGST;
$sgst_per=$product_det->SGST;
$igst=0;
}
echo $rate."-".$amount."-".$cgst."-".$sgst."-".$igst."";
}
My javascript function:
$.ajax({
url: '<?php echo base_url(); ?>sales/product_det',
data: ({"item_id":item_id,"quantity":quantity,"rate":rate }),
dataType: 'html',
type: 'post',
success: function(data)
{
alert(data);
}
});
You are using result_array() in your sql query.
Then use foreach loop to show your record like this
if(!empty($product_det))
{
foreach($product_det as $p_det){
if($rate=="" || $rate==0)
{
$rate=$p_det['sale_rate'];
}
$amount=$rate*$quantity;
$cgst_per=$p_det['CGST'];
$sgst_per=$p_det['SGST'];
$igst=0;
}
}
Since apparently you only need details of one product.
Change:
$res=$this->db->get()->result_array();
To:
$res=$this->db->get()->row_array();
Then, the result is an array not an object. To access the returned elements do the following instead.
Change:
$product_det->sale_rate;
$product_det->CGST;
$product_det->SGST;
To:
$product_det['sale_rate'];
$product_det['CGST']
$product_det['SGST'];
Hope this will help you :
In model just Replace
$this->db->get()->result_array();
With
$this->db->get()->row();
Your model should be like this :
function get_productDet($item)
{
$this->db->select('*');
$this->db->from('product');
$this->db->where('id',$item);
$res = $this->db->get()->row();
return $res;
}

Call function before return Response::json in Laravel 4 gives "json.parse:unexpected non-whitespace character after json data"

My Controller
public function electricityBillAddToCart(){
$accountNumber = Input::get( 'accountNumber' );
$amount = Input::get( 'amount' );
$userId = Auth::user()->id;
$type = TransactionTypes::ELECTRICITY;
if(is_numeric($accountNumber)) {
CartHelper::addToCart($userId, $accountNumber, $amount, $type);
return Response::json("Okay");
}
return Response::json("Error");
}
The above function is called using the following ajax request
$.ajax({
url: "/addToCart/electricityBill",
type: "POST",
dataType:"json",
data: {'accountNumber': reloadto, 'amount': amount},
success: function (re) {
console.log("Success");
},
error: function(re) {
console.log("Error");
}
The response to the front end would be []"okay", and "Error" would be printed in the console. If the static function that I call before sending the response is commented out it would work fine (it would return "okay", and print "Success"). The code that I comment is
CartHelper::addToCart($userId, $accountNumber, $amount, $type);
Anyone knows why this is?
I found the problem. There was an echo in the CartHelper class. Once that was deleted, it worked fine

Categories