cakephp ajax function issue - ajax

I have 2 functions in "events" (index, event_ajax)controller in my cakephp(2.5) web site. I'm trying to load HTML block to 'index.ctp' page by calling to 'event_ajax' function using ajax. When I call to this function it shows nothing. Look at 'net' tab in firebug it shows internal server error and 'net'->'Response' tab I can see whole layout is loaded.
I'm little confuse about in this scenario, can any one give a little explanation for following questions??? thanks in advance :)
Is it possible to call actions in same controller using ajax function ??
How 'Response' tab shows layout when '$this->layout' is set to NULL ??
when type url 'example.com/events/event_ajax', output data still '$this->autoRender=false'. how can this happen ??
this is my 'event_ajax' action.
public function event_ajax($x=1) {
$this->layout = NULL;
$this->autoRender = false ;
$contName = $this->Page->conName($x);
$latestContEvents = $this->Page->latestContEvent($x);
$internal = '';
if (!empty($latestContEvents)){
foreach ($latestContEvents AS $latestContEvent){
$internal .= '<li class="pull-left"> <div class="content-wrapper">'..... //do something
}
else {
$internal = '<p> No events found for this continent</p>';
}
$ContEvents = '<div class="carousel events-location-carousel">'.$internal.'</div> ';
return $ContEvents;
// return json_encode($ContEvents);
}

Try with
$this->layout = 'ajax';

Related

Saving the page state after choosing filters

I have some filters on a web page (checkboxes) and I modify the result list by ajax POST method. Is there a way that somehow I save the page state and send the URL link to someone so they open it in that state? Any help is appreciated.
By the way I'm using Laravel.
You can use parameters :
test.com/page?checkbox1=checked&checkbox2=checked
In your Laravel controller you can do this :
public function page($request) {
$checkboxes = array();
if ($request->has('checkbox1')) {
$checkboxes[] = true;
}
if ($request->has('checkbox2')) {
$checkboxes[] = true;
}
// ... and so on.
return view('page', compact('checkboxes'));
}
And set your php page like this :
<input type="checkbox" <?php checkboxes[$i++] ? 'checked' : '' ?> >
You can set the checkbox as parameter in the URL, and when the user go to your address, check if there is any of your params.
if so - set the checkboxes as you wish
just to get the general idea..
function getUrlParams(requested_param){
//check for params
return result;
}
if (getUrlParams(myCheckBox)){
$('#checkbox_on_page').prop( "checked", true );
}

How to using jtable plugin in codeigniter?

I'm trying to use jtable plugin in framework codeigniter but i got a problem. I'm confused how to pass variable from view (jtable javascript code) to controller and to pass json_encode from controller to view.
Here are my code.
in my view page(Attendance_view.php).
[html code]
<input style="width:100px" type="text" id="from" name="from" value="<?php echo date("Y-m")."-01";?>">
[js code]
//Prepare jTable
var base_url ="<?=base_url()?>";
$('#TableContainer').jtable({
title: 'Attendance',
paging: true,
sorting: true,
defaultSorting: 'month ASC',
selecting: true, //Enable selecting
multiselect: true, //Allow multiple selecting
selectingCheckboxes: true, //Show checkboxes on first column
actions: {
listAction: '<?=base_url()?>index.php/Attendance_controller/listRecord',
createAction: '<?=base_url()?>index.php/Attendance_controller/create',
updateAction: '<?=base_url()?>index.php/Attendance_controller/update',
deleteAction: '<?=base_url()?>index.php/AttendanceAbsensi_controller/delete'
},
....//another field here
});
//Load attendance from server
$('#TableContainer').jtable('load',{
month:$("#from").val()
});
in my controller(Attendance_controller.php)
function listRecord()
{
$this->load->model('Attendance_action');
$jTableResult=$this->Attendance_action->list_record();
$data['jTableResult']= json_encode($jTableResult);
$this->load->view('Attendance_view',$data['jTableResult']);
}
in my model (Attendance_action.php)
function list_record()
{
//get post variable
$date=$this->input->post('month'); // i can't get the value.
//Get record count
$result = //my query here[select "some data" from "mytable" where month='$date']
$recordCount = mysql_num_rows($result);
//Add all records to an array
$rows = array();
while($row = mysql_fetch_array($result))
{
$rows[] = $row;
}
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
$jTableResult['TotalRecordCount'] = $recordCount;
$jTableResult['Records'] = $rows;
return $jTableResult;
}
When i load the controller page, the error message from jtable occured "An error occured while communicating to the server". Please help. thanks.
Why your using jtable .can you use Jquery Datatbles here having ignited Datatables Library using that one we can implement crud functionality simple
you can interest please check it once the bellow url
https://github.com/IgnitedDatatables/Ignited-Datatables/
http://datatables.net/examples/data_sources/server_side.html
https://ellislab.com/forums/viewthread/160896/
i am personal y like jquery data-tables.just check it once
First load view page in separate function .In that page you call call your crud Urls
Change like this your controller
function list()
{
$this->load->view('Attendance_view');
}
function listRecord()
{
$this->load->model('Attendance_action');
$jTableResult=$this->Attendance_action->list_record();
print_r(json_encode($jTableResult));
}
Note : Can you please check it once this here mentioned clearly how to implement this one
http://jtable.org/GettingStarted
i want to share my code. Now the problem fixed. I just change the controller code like this. Add "exit();" in controller.
function index()
{
$this->load->view('Attendance_view');
}
function listRecord()
{
$this->load->model('Attendance_action');
$jTableResult=$this->Antendance_action->list_record();
print_r(json_encode($jTableResult));
exit();
}

html is pass using jquery ajax but pdf is not generating in mpdf for codeigniter

I want to create a PDF in codeigniter using mPDF. My html is passed to the controller using jQuery AJAX. Data is coming to the $html But it is not working. It works fine when html is hard coded. Can any one help me please?
public function pdf($paper='A4')
{
$html = '';
$html = $this->input->POST('content');
$this->load->library('mpdf54/mpdf');
$CI->mpdf = new mPDF('utf-8',$paper);
$mpdf->debug = true;
$this->mpdf->WriteHTML($html);
$this->mpdf->Output();
exit;
}
Try grabbing all the POST vars by using
$html = $this->input->POST();
then echo those out to yourself before moving farther to be sure they are getting set.
public function pdf($paper='A4')
{
print_r($this->input->POST());
return;
}
This of course is only for testing but might help you to see why your $html var isn't getting set. Try that out and give us the results.

Zend Form: onchange select load another view content

In my application I have a form in controller/index that consists out of 3 select boxes. When all three boxes have a value selected I need to show additional html and extra form options in the same view based on those select values. The obvious solution seems to make an ajax call to another action that handles the database operation and creates a view and loading that view into the controller/index.phtml
I have been able to load a view of another action in the index.phtml by using:
$('#select').change(function() {
event.preventDefault();
var id = $(this).attr('id');
$('#results').show();
$('#results').load('/controller/index/' + $(this).attr('value'));
return false;
});
However I need to pass the variables of all three select boxes and for that I alternatively used:
$('#select1').change(function() {
var select1 = $('#select1').val();
var select2 = $('#select2').val();
var select3 = $('#select3').val();
$.ajax({
type: 'POST',
dataType: 'json',
url: '/controller/index/',
data: { select1: select1, select2: select2, select3: select3},
success: function(result){
var return1 = result.return1;
var return2 = result.return2;
}
});
});
The last method works in as far that I do see the variables passed in the headers and the response contains the view, but I cant fix it that just the content of the ajax view is placed within the index view. (Ofcourse by not using AjaxContent switching, the ajax view will load but that includes the complete layout as well.) Anything that I echo in the ajax action or ajax view do not show in the index view. Any pointer would be more than welcome
EDIT
the ajax action now looks like
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$select1 = $this->_request->getParam('select1');
$select2 = $this->_request->getParam('select2');
$select3 = $this->_request->getParam('select3');
// DO THE OTHER STUFF AND LOGIC HERE
$results = array(
'return1' => 'value1',
'return2' => 'value2'
);
$this->_response->setBody(json_encode($results));
and the controller init
public function init() {
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('ajax', 'json')->initContext();
}
So everything works, I can see the returned values in the response by using developer tool (network) in my browser, however I just do not know how I can use this to "update" the view
You can do two things:
You can enable the layout of the action you are calling via ajax. See you have disabled layout so even if the view phtml file of the ajax action contains something, it won't show. You can enable layout, use text/html dataType instead of json and show the returned HTML somewhere.
Or, in the success event of the ajax call, write javascript codes to update DOM.
Thanks #Salman for your suggestions as they lead me in the right direction and I managed to solve the problem.
I managed to pass multiple parameters with the ajax .load() call by passing them as get parameters.
The results of the ajaxAction could then be formatted in the ajax.ajax.phtml view and were consecutively
shown within the #results div that resides in the index.phtml where the select boxes are.
controller/index.phtml
<div id="results" style="display:block;">Select all three values</div>
IndexController init and ajaxAction
public function init() {
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('ajax', 'html')->initContext('html');
}
public function ajaxAction() {
$select1 = $this->_request->getQuery('select1');
$select2 = $this->_request->getQuery('select2');
$select3 = $this->_request->getQuery('select3');
$form = new Application_Form();
// Database operations and logic
$this->view->form = $form;
$this->view->array = $somearray;
}
}
jquery script in index.phtml
$(document).ready(function(){
$('.selector').change(function() {
var select1 = $('#select1').val();
var select2 = $('#select2').val();
var select3 = $('#select3').val();
if ( select1 && select2 && select3) {
$('#results').show();
$('#results').load('/controller/ajax?select1=' + select1 + '&select2=' + select2 + '&select3=' + select3);
}
});
});
controller/ajax.ajax.phtml
<?php if ( $this->array ) : ?>
<?php echo( $this->form ); ?>
<?php else: ?>
Nothing found for selected values
<?php endif ?>

one dropdown appears on the selection of other dropdown using codeigniter

I am trying to create dynamic dropdown of category and on selection of this category sub-category dropdown should appear. I have done this using OOP PHP but really having tought time with codeigniter.
Firstly I have created this category dropdown
<?php
$js =' onChange="callAjaxFunction(this.value)"';
echo form_dropdown('category', $categories, null, $js); ?>
Javascript to show sub-category
<script>
// JavaScript Document
var enableCache = false;
var jsCache = new Array();
var AjaxObjects = new Array(new sack(),new sack());
function ShowContent(divId,ajaxIndex,url)
{
document.getElementById(divId).innerHTML = AjaxObjects[ajaxIndex].response;
if(enableCache){
jsCache[url] = AjaxObjects[ajaxIndex].response;
}
AjaxObjects[ajaxIndex] = false;
document.getElementById("ajax_container").innerHTML = '';
}
function ShiftChanger(divId,url,id) {
//to show the div
document.getElementById(divId).innerHTML="";
if(enableCache && jsCache[url]){
document.getElementById(divId).innerHTML = jsCache[url];
return;
}
var ajaxIndex = AjaxObjects.length;
AjaxObjects[ajaxIndex] = new sack();
AjaxObjects[ajaxIndex].requestFile = url+"?id="+id;
document.getElementById("ajax_container").innerHTML = '<img src=ajax_loader.gif hspace=10 vspace=10 />'; AjaxObjects[ajaxIndex].onCompletion = function(){ ShowContent(divId,ajaxIndex,url+"?id="+id);};
AjaxObjects[ajaxIndex].runAJAX();
}
function callAjaxFunction(value)
{
ShiftChanger('shiftcontainer','ajax_category.php',value);
}
</script>
But this is not working. As you can see ajax_category.php is not being able to pass. I think it should be done with controller, I had tried that also but nothing seems as working. I am really stuck with this. Please anyone with little help. Really getting depressed with it :(.

Resources