I have a problem in YII with ajax.
I used this link as exaple to test ajax in my project with depended drop-down lists.
Form code (protected/views/game/_form.php):
<?php
echo $form->dropDownList($model, 'season_id', Season::getSeasonsList(), array(
'ajax' => array(
'type'=>'POST',
CController::createUrl('Game/selectGameStages'),
'update'=>'#Game_season_game_stage'
)
)
);
?>
Controller code (protected/Controller/GameController.php):
public function actionSelectGameStages()
{
echo CHtml::tag('option', array('value'=>'1'), 'Some output 1', true);
echo CHtml::tag('option', array('value'=>'2'), 'Some output 2', true);
echo CHtml::tag('option', array('value'=>'3'), 'Some output 3', true);
}
Ajax is working, but with debugger if founded that code, putted in my Game_season_game_stage select is the html code of the whole page of my site, like as
<select name="Game[season_game_stage] id="Game_season_game_stage">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
<div class="container" id="page">...</div>
Please help to understand why whole code of page loaded instead of code from my selectGameStages function?
Yii version is 1.14.
Sorry for my English. Thx.
Simply exit after the output.
public function actionSelectGameStages()
{
// Do stuff
...
exit;
}
why noy use dropDownList and directly in the view?
es.
<?php echo CHtml::dropDownList('listname', $select,
array('M' => 'Male', 'F' => 'Female'),
array('empty' => '(Select a gender)'));
http://www.yiiframework.com/wiki/48/by-example-chtml/#hh5
Thank you for all answers! I found solution.
in protected/views/game/_form.php I missed the 'url' key and renamed game/selectGameStages
<?php
echo $form->dropDownList($model, 'season_id', Season::getSeasonsList(), array(
'ajax' => array(
'type'=>'POST',
'url' => CController::createUrl('game/selectGameStages'),
'update'=>'#Game_season_game_stage'
)
)
);
?>
after that I updated controller code (protected/Controller/GameController.php) with this code
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view', 'selectGameStages'),
'users'=>array('*'),
Thanks for the help!
Your whole page is loading because of your layout,
in your controller check if this call, is an ajax call, change your layout to some layout without those tags, or better create a layout for your ajax calls,
if($this->getIsAjaxRequest())
$this->layout = '//ajax'; // ajax layout
Related
I have problem with AJAX in CakePHP. I have 2 different AJAX forms in one view. The first AJAX form works well, but the other one doesn't work properly. When I call the second AJAX, it doesn't set data from this form to $this->request->data, but the AJAX will run properly. After that, it will update <div id="about"></div>. Curious is fact, that after the div is updated and I try send data through this form again, it works and it will update my data in database. Also when I have only the second AJAX in the view, it will work properly and send the data first time I send the data through it.
Here's my code:
This is a view file:
<div id="price-list">
<?php echo $user['User']['price_list']; ?>
</div>
<?php
echo $this->Form->create('User');
echo $this->Form->input('User.price_list', array('label' => false));
echo $this->Js->submit('Save', array(
'url' => array('controller' => 'users', 'action' => 'ajax_edit_price_list'),
'update' => '#price-list',
'buffer' => false,
));
?>
<div id="about">
<?php echo $user['User']['about']; ?>
</div>
<?php
echo $this->Form->create('User');
echo $this->Form->input('User.about', array('label' => false));
echo $this->Js->submit('Save', array(
'url' => array('controller' => 'users', 'action' => 'ajax_edit_about'),
'update' => '#about',
'buffer' => false,
));
?>
I have also append <?php echo $this->Js->writeBuffer(); ?> in my default.ctp before </body>.
Do you have any idea, where can be a problem? Thanks
I wasn't able to reproduce your exact issue, but I think I know the problem. Your second form is being created inside of the first form. You need to add echo $this->Form->end(); to the end of both forms.
Also, the forms are getting created with the same ID. Although this isn't what's causing the problem, it's still not good. I suggest you take thaJeztah's advice and specify an ID manually inside $this->Form->create().
I have more then one forms on one page and I am rendering one form using renderPartial, now if I want to validate it using ajax validation it don't work.
view code
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'bill-shipp-form',
'action'=>CController::createUrl('cart/index'),
'enableAjaxValidation'=>true,
'focus'=>array($billingShippingInfo,'first_name_b'),
//'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange'=>false,
'afterValidate'=>'js:postBillShipp'
),
)); ?>
and in cart/index I have
if(isset($_POST['ajax']) && $_POST['ajax']==='bill-shipp-form')
{
echo CActiveForm::validate($billingShippingInfo);
Yii::app()->end();
}
Thanks in advance
You should use 4th parameter for render partial:
$this->render('view',$data,false,TRUE);
The 4th paramater is processOutput and you should set it to true
API
here is what i am trying to achieve...
i have a index view in which i have used the ajax helper not the cakephp one but an external one from http://www.cakephp.bee.pl/
i have succefully created an accordion using this helper.
now inside the accordion i have an addbutton which submits a form via ajax.
what i would like to do is when i submit the ajax form. i would also like to update the accordion.
now the accordion is also created by using the ajax helper.
Can this be done without a page refresh
ajax submit form code :
<div class="qnotesform">
<?php echo $this->Form->create('Qnote');?>
<ul class="qnotelist">
<li><?php echo $this->Form->input('Qnote.subject', array('label'=>'Title', 'rows' => '1')); ?></li>
<li> <?php echo $this->Form->input('Qnote.body', array('label'=>'Header', 'rows' => '1')); ?></li>
<?php echo $this->Form->hidden('Qnote.user_id', array('value'=>Authsome::get('id'))); ?>
<?php echo $this->Form->hidden('Step.0.user_id', array('value'=>Authsome::get('id'))); ?>
<li><?php echo $this->Form->input('Step.0.body', array('label'=>'Steps' ,'class' => 'step', 'rows' => '1')); ?></li>
</ul>
<?php echo $ajax->submit(
'Submit', array(
'url' => array(
'controller'=>'qnotes',
'action'=>'add')
));
how can i achieve this .
could someone please point me the right direction
another thing that i want to clear out is. If i want to be working one page . and all crud
action to be used without a page refresh.
does anybody have a working example for the above senarios .
or let me make it simple.
how to refresh a div without refreshing the page.
Solution:
code
<?php echo $ajax->submit('Submit', array(
'url'=> array(
'controller'=>'qnotes',
'action'=>'add'),
'update'=>'leftdiv',
'after'=>'$("#accordion").accordion({autoHeight: false, collapsible: true , header: "h3"});alert("after");',
));
however now I am facing another issue now,
I am updating the accordion, everything works fine but as soon as update the accordion.
the accordion loses its style class and stops working.
I only see links then. any idea how.
It seems that one of the options in the array of the submit method is 'complete' which should allow you to trigger a js function that will update your accordion:
<?php echo $ajax->submit(
'Submit', array(
'url' => array(
'controller'=>'qnotes',
'action'=>'add',
'complete' => 'updateAccordian(request.responseText)')
));
the reuqest.responseText will contain the reponse from your controller/action.
HTH
I am running an ajax submit in cakephp. It appears to be running, but the end result for the population of the assigned div is the page itself as opposed to the results set. And it also says the controller is missing which obviously is not true.
Model: Plan
Action: search()
search form element (being pulled into the search.ctp):
...
echo $ajax->form(array('type' => 'post',
'options' => array(
'update' => 'plansQueryResults',
'url' => array('controller' => 'plan', 'action' => 'search'),
'loading' => "Element.show('plsLoaderID')",
'loaded' => "Element.hide('plsLoaderID')"
)
));
...
echo $form->end();
This div is pulling in the default layout, but
<div id="plansQueryResults"></div>
Standard error (wrapped around default layout) displaying within the div:
Missing Controller
Error: PlanController could not be found.
Error: Create the class PlanController below in file: dental/1/app/controllers/plan_controller.php
<?php
class PlanController extends AppController {
var $name = 'Plan';
}
?>
What is the code you have on the search action?
What is the returned data from the action? Check with Firebug.
By the way... when following the standards to create a DB, it is better and handy use cake bake: Code Generation with Bake
I'm trying to add ajax pagination into a view of my application.
I followed this tutorial: http://book.cakephp.org/view/1600/Ajax-Pagination
So, in this tutorial you always reload the whole content of the view by clicking the pagination links.
But what I want is, that it only reloads the part of the site which includes the paginated data.
I found a solution for myself:
I made a view which only contains the ajax pagination and included it via ajax request:
<?php $this->Html->scriptStart(array('inline' => false));?>
$(document).ready(function() {
<?php
echo $this->Js->request(array(
'controller' => 'topics',
'action' => 'index',
$location['Location']['id']
), array(
'async' => true,
'update' => '#ajax_topics',
));
?>
});
<?php $this->Html->scriptEnd();?>