yii CHtml::button and POST request to controller - yii

Is there a way to have CHtml::button send a post request to the controller
<?php echo CHtml::button('Button Text', array('submit' => array('controller/action'))); ?>
I'm looking to replicate the CHtml::linkfunctionality and POST to the controller
<?php echo CHtml::link('Delete',"#", array("submit"=>array('delete', 'id'=>$data->ID), 'confirm' => 'Are you sure?')); ?>
EDIT:
The button is NOT submitting a form

Try this:
echo CHtml::button('Delete',
array(
'submit'=>array('controllername/actionname',array('id'=>$id)),
'confirm' => 'Are you sure?'
// or you can use 'params'=>array('id'=>$id)
)
);
As you'll see button also takes the special htmlOptions attribute clientChange.
Update Clarification of submit, from the doc link ():
submit: string, specifies the URL to submit to. If the current element has a parent form, that form will be submitted, and if 'submit' is non-empty its value will replace the form's URL. If there is no parent form the data listed in 'params' will be submitted instead (via POST method), to the URL in 'submit' or the currently requested URL if 'submit' is empty. Please note that if the 'csrf' setting is true, the CSRF token will be included in the params too.
emphasis mine
As you mentioned that you want to hit the delete action, the default gii generated actionDelete expects the id in the url, hence I passed the id in the url, i.e submit option.

Related

Yii: Same route but only GET works after confirming there's no controller filter

My urlManager rules: (basically the one comes default)
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',`
My controller:
class SiteController extends Controller {
public function actionSubscribe() {
echo 'gdg';
die();
}
}
My view:
<form method="POST" action="<?php echo $this->createUrl('site/subscribe'); ?>" style="display: inline;">
<input style="margin: 0 18px 0 6px;" type="text" value="e-mail"/>
</form>
When I access it using the url http://localhost/site/subscribe directly it works, but when I type something in the text field and push my enter button to post the form it says The system is unable to find the requested action "error".
I'm very certain that it has something to do with my form. I have so far no problem using active form but for this form I don't have a model and I don't want to use form builder. Any help?
"When I access it using the url http://*/site/subscribe directly it works"
=> You execute subscribe action with GET method.
"but when I type something in the text field and push my enter button to post the form it says The system is unable to find the requested action "error"."
=> You execute subscribe action with POST method.
=> There are some errors and I think Yii try to handle error with your default configure:
return array(
......
'components'=>array(
'errorHandler'=>array(
'errorAction'=>'site/error',
),
),
);
However, Yii can't find error action with your SiteController so it throws The system is unable to find the requested action "error". You can add error action to see information about errors like:
public function actionError()
{
if($error=Yii::app()->errorHandler->error){
$this->render('error', $error);
}
}
More info: The above error variable is an array with the following fields:
code: the HTTP status code (e.g. 403, 500);
type: the error type (e.g. CHttpException, PHP Error);
message: the error message;
file: the name of the PHP script file where the error occurs;
line: the line number of the code where the error occurs;
trace: the call stack of the error;
source: the context source code where the error occurs.

Yii - Ajax Get based on drop down selection

I am trying to display the price based on the selection of product from a drop down.
View contains:
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'plan-form',
'enableAjaxValidation'=>true,
)); ?>
<?php echo $form->dropDownListRow($model, 'plan_id',
$planList, array('id'=>'planid', 'prompt'=>'Select Plan',
'ajax' => array('type'=>'GET',
'url'=> Yii::app()->createUrl('mbr/plan/ajaxGetPrice'),
'update'=>'#price'))); ?>
<div id="price">0.00</div>
<?php $this->endWidget(); ?>
Action:
public function actionAjaxGetPrice()
{
Yii::log("Within AjaxGetPrice");
if (Yii::app()->request->isAjaxRequest)
Yii::log("ajax request");
else
Yii::log("regular request");
//$plan=Plan::model()->findByPk((int) $_GET['plan_id']);
//Yii::log(serializ($plan));
// echo $plan->price;
echo "10";
Yii::app()->end();
}
It is not updating the price. Its not calling the action at all. I looked at the suggestions found in Yiiframework and here and tried those and still no luck.
when I add this to the view
<?php
echo CHtml::ajaxLink(
"Get Price",
Yii::app()->createUrl('mbr/plan/ajaxgetprice'),
array( // ajaxOptions
'type' => 'GET',
'update' => '#price'),
array( //htmlOptions
'href' => Yii::app()->createUrl('mbr/plan/ajaxgetprice')
)
);
?>
I get the response when I click on the link, but it renders a separate page with value "10". I have URLFormat = Path.
What am I doing wrong? Any pointers?
You can also try this as shown
<?php echo CHtml::dropDownList('categories','',
$category,
array('ajax'=>array('type'=>'POST','url'=>CController::createUrl('yourController/GetId'),'update' =>'#data'))
);?>
yourController action
public function actionGetId(){
echo 10;
}
And the div
<div id="data">0.00</div>
I'm not really sure how to do this the Yii way but the following javascript should be useful.
$("#yourdropdownidhere").change(function(){
$.ajax({
url: 'your url here', //(dynamically generated by product item)
type: "get",
success: ajaxSuccessHandler
});
});
function ajaxSuccessHandler(obj) {
$('#price').html(obj);
}
I also suggest sending a JSON object as the response for AJAX requests, so it will have an actual structure. A great blog post on this to check out would be Fully ajax website with Yii
I had the same problem and solved it on the controller that renders the view. If you are using renderPartial assure that the processOutput() option is set to TRUE, otherwise Yii will not attach any Javascript.
If you use render instead of renderPartial it also works.

enableAjaxValidation is not working cactiveform in yii

I am using both the client validation and ajax validation for my CActiveForm but both the client and ajax validations are not performing if i remove ajaxvalidation means to false the client validation is happening. can any suggest for the solution here i am posting my code
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'useraccess-form',
'enableClientValidation'=>true,
'enableAjaxValidation'=> true,
'clientOptions' => array(
'validateOnSubmit' => true
),
)); ?>
I got the same problem when tried to set id for feilds via htmlOptions
Check source code of generated page. Compare ids of fields in javascript code with ids in html code
I am included JQuery twice. Need once and with framework.
<?php Yii::app()->getClientScript()->registerCoreScript('jquery'); ?>

yii Tipsy tooltip css does not work while using in ajax popup?

I am using Tipsy tooltip
in a popup which loads on ajax call. In such case the tooltip css does not work. The widget load the css file but does not work.
Here is my code :
<a id="north-west" href="#" original-title="Click on this">Click me</a>
<?php
$this->widget('application.extensions.tipsy.Tipsy', array(
'trigger' => 'hover',
'items' => array(
array('id' => '#north-west', 'gravity' => 'sw'),
),
));
?>
Usually when you load via ajax you would use 'renderPartial' instead of 'render'. There is one catch though: If you have javascript in it you need to make sure the processOutput parameter is set to TRUE. Basically the call would be:
$this->renderPartial('view', array(<data>), FALSE, TRUE);
Yii would then make sure the javascript is also returned, it normally does not do that with a partial. This is only required for ajax calls where the renderPartial is the only output you do, not for calls to it during a regular request.

How to create ajax delete link in CViewList widget in _view file

CGridView widget is already having view,update,dete option.But i am using CListView widget in my jquery mobile based project, but having problem in creating ajax link for delete option. Not getting idea how to create a ajax delete link in _view.php(view file) and its renderPartial() view file to disappear the bar after successfully deleted plz help thanks in advance. Here is the _view.php file link for edit and delete.
<?php
echo CHtml::link(CHtml::encode($data->id),
array('editmember1', 'id' => $data->id),
array('data-role' => 'button', 'data-icon' => 'star')
);
echo CHtml::link(CHtml::encode($data->id), $this->createUrl('customer/delete', array('id' => $data->id)),
array(
// for htmlOptions
'onclick' => ' {' . CHtml::ajax(array(
'beforeSend' => 'js:function(){if(confirm("Are you sure you want to delete?"))return true;else return false;}',
'success' => "js:function(html){ alert('removed'); }")) .
'return false;}', // returning false prevents the default navigation to another url on a new page
'class' => 'delete-icon',
'id' => 'x' . $data->id)
);
?>
This is happening because:
The correct action is not being called, because you have not set the url property of jQuery.ajax(). You should know that Yii's CHtml::ajax is built on top of jQuery's ajax. So you can add :
CHtml::ajax(array(
...
'url'=>$this->createUrl('customer/delete', array('id' => $data->id,'ajax'=>'delete')),
...
))
Also in the url i'm passing an ajax parameter so that the action knows that it's an ajax request explicitly.
Then the controller action by default(i.e Gii generated CRUD) expects the request to be of post type, you can see this in the customer/delete action line:if(Yii::app()->request->isPostRequest){...}. So you have to send a POST request, again modify the ajax options:
CHtml::ajax(array(
...
'type'=>'POST',
'url'=>'',// copy from point 1 above
...
))
Alternatively you can also use CHtml::ajaxLink().
To update the CListView after deletion, call $.fn.yiiListView.update("id_of_the_listview");. Something like:
CHtml::ajax(array(
...
'type'=>'POST',
'url'=>'',// copy from point 1 above
'complete'=>'js:function(jqXHR, textStatus){$.fn.yiiListView.update("mylistview");}'
...
))