I'm currently using the following code to send an Ajax get request to my controller:
echo CHtml::ajaxLink('clickMe', array('ajax'), array('update'=>'#results'));
This works fine, the controller receives the request and updates the view accordingly.
Now, I want to send in this request attributes of the model, i.e. from model->getAttributes();
How should I do this? Create a JSON object of the attributes and send that with the request?
Just pass 'data' attribute and 'type' if needed:
echo CHtml::ajaxLink('clickMe', array('ajax'), array(
'update' => '#results'
'data' => CJSON::encode($model->attributes),
'type' => 'post',
));
This code just replaces #results contents with json. If you need something different, use 'success' instead of 'update' like this:
echo CHtml::ajaxLink('clickMe', array('ajax'), array(
'success' => 'function (response) {
// do everything you need
}',
'data' => CJSON::encode($model->attributes),
'type' => 'post',
));
Take a look at jquery ajax options for more information.
Related
I use REST api for CRUD.
When I Use return redirect after CRUD, Laravel does not send me variable with
I don't want use session.
My code
return redirect("inventories")->with([ 'Tab' => 5,'flag' => 'success', 'flagMsg' => 'delete_successful', 'Inventories' => $Inventory]);
and when I use return view, Laravel send me variable but URL not change
and I want redirect and change URl
for example my URL is /inventories/2/edit and after update send me to /inventories/update and I want to redirect inventories
you can use link_to_route() and link_to_action() methods too. (source)
link_to_route take three parameters (name, title and parameters). you can use it like following:
link_to_route('api.GetAPI', 'get api', [
'page_no' => $page_no,
'id' => $id
]);
If you want to use an action, link_to_action() is very similar but it uses action name instead of route.
link_to_action('ApiController#getApi', 'get api', [
'page_no' => $page_no,
'id' => $id
]);
I've the page, which loads content by click on the button from user database via jquery ajax.
File, which handles ajax request, has this code:
return $modx->runSnippet('pdoPage',array(
'class' => 'LibraryContent',
'tpl' => 'tpl.lib-main',
'element' => 'Ajax_test'
));
Sender of request receives answer, snippet works ok, I see data, which snippet returns. But, what about placeholders? For example, I want use pagination by this snippet(pdoPage). I cannot send placeholders via ajax, because modx parser have been worked and placeholder will be a plain text. Other path is paste placeholder to resource, which send ajax request, but no way — there is placeholder, but there is no results of pdoPage's work. Finally, placeholder is empty.
So, my question is how to "alive" placeholder for snippet, which loads by ajax request?
thanks.
1'st way:
$output = $modx->runSnippet('pdoPage',array(
'class' => 'LibraryContent',
'tpl' => 'tpl.lib-main',
'element' => 'Ajax_test'
));
$pagination = $modx->getPlaceholder('page.nav');
return $output.$pagination;
2'nd way:
$content = $modx->runSnippet('pdoPage',array(
'class' => 'LibraryContent',
'tpl' => 'tpl.lib-main',
'element' => 'Ajax_test'
));
$pagination = $modx->getPlaceholder('page.nav');
$output = array(
'content' => $content,
'pagination' => $pagination
);
return $modx->toJSON($output);
I have two ajax in my code. One of them works but the other doesn't send data to the controller.
The one that works is an ajax button:
echo CHtml::ajaxSubmitButton(Yii::t('wii','Show'),
CHtml::normalizeUrl(array("general/ajax&render=false")),
array(
'data'=>'js:$("#general-form").serialize()',
'success'=>'js:function(data){
$("#question-grid").html(data);
$(\'select[name="Question[difficultly]"]\').trigger(\'change\');
}',
),
array(
'id'=>'ajaxSubmitBtn',
'name'=>'ajaxSubmitBtn',
'live'=>false
));
And the one that doesn't work is an ajax link:
echo CHtml::ajaxLink(Yii::t('wii','Show Questions'),
array('question/show&id='.$exam),
array(
'type' =>'POST',
'dataType'=>'json',
'data'=>'js:$("#general-form").serialize()',
'success'=>'js:function(data){
$("#question-grid").html(data);
$(\'select[name="Question[difficultly]"]\').trigger(\'change\');
}',
),
array(
'class'=>"smoth",
'href'=>Yii::app()->createUrl('question/show&id='.$exam)
));
But these two are similar. What could be the problem?
I am generating a checkbox using the corresponding method of CHtml, and I want to run some JavaScript code before and after the AJAX request. Here is the code:
echo CHtml::checkBox('markComplete', FALSE,
array(
'class' => 'markComplete',
'ajax' => array(
'type' => 'POST',
'url' => $this->createUrl('/events/events/MarkComplete'),
'data' => 'event_status='.$events['id'],
'beforeSend' => 'function(){ $(this).parent("TR").hide(); }',
'success' => 'function(resp) { $("#right").append(resp); }'
),
)
);
How can I tell Yii that beforeSend and success are JavaScript code and not plain strings?
Better option available starting from Yii 1.1.11
All framework classes now support custom JavaScript snippets through CJavaScriptExpression. Use it like this:
'ajax' => array(
'beforeSend' => new CJavaScriptExpression(
'function(){ $(this).parent("TR").hide(); }'
),
// ...
)
The option of prefixing strings with js: is still available by default, but it can now be disabled as required using an optional parameter on CJavaScript::encode.
Original answer
If you want to include literal JavaScript code as part of options, the convention in Yii is to prefix the code with js:. So you would write it like this:
'ajax' => array(
'type'=>'POST',
'url'=>$this->createUrl('/events/events/MarkComplete'),
'data'=>'event_status='.$events['id'],
'beforeSend' => 'js:function(){
$(this).parent("TR").hide();
}',
'success'=>'js:function(resp) {
$("#right").append(resp);
}'
),
)
Unfortunately this is not well documented, which is why people run into exactly this problem every now and then.
When you create an ajax button like
echo CHtml::ajaxSubmitButton(
'Submit request',
array('logInOfficeEmp/update&id='.$model->id),
array(
'replace'=>'#req_res02',
array('id' => 'inside_popup') // how to retrieve this
)
);
How to retrieve the ID in the controller, note that you get to know it's ajax or not using Yii::app()->request->isAjaxRequest()
Pass it as follows:
<?= CHtml::ajaxButton('Click', $this->createUrl('path/here', array('myparam' => 'test)), array('success' => 'js:function(){alert("test");}'), array('class' => 'cssclasses'))?>
It will then be available in $_POST['myparam']