ASK:: Parsing function result to textfield using AJAX in Yii - php

I have a dropdownlist and a textfield.
If the value of dropdownlist is change, I want to call an action in controller and parsing the result to textfield value.
How can I do that?
Here is my View:
echo TbHtml::dropDownList('isr[setting_merit_demerit_id]', $isr['setting_merit_demerit_id'], $this->getMeritList(),
array('class'=>'span4', // 'onchange' => 'generateNumber();',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('getMeritDemeritStatus'),
// 'success' =>'js:function(result){$("#tes").val(result).change();}', <--- WHAT SHOULD I PUT HERE?
'data' => array('setting_merit_demerit_id' => 'js:this.value'),
)
)
);
echo TbHtml::textField('isr[show_in_reportcard]',$isr['show_in_reportcard'], array('class'=>'span1', 'id'=>'tes'));
Here is my Controller:
function actionGetMeritDemeritStatus(){
$setting_merit_demerit_id = $_POST['setting_merit_demerit_id'];
$model= SettingMeritDemeritM::model()->findByPk($id);
return $model->status;
}
Sorry for my bad english.. thanks

Your first problem is that you are sending an AJAX request and calling a function that just returns a model. You would have to json_encode return value and then echo it. On the receiving end you would have to, in js: success, parse result and then work with that object and assign its properties as values of text fields.
Hope it helps.

Related

Keep checked value of Yii radiobuttonlist after submit

I'm using a radioButtonList like this one:
$form->radioButtonList(Store::model(), 'product',
array(CODE1 => TEXT1,
CODE2 => TEXT2,
CODE3 => TEXT3)
);
This radioButtonList is part of a form with more fields. After submiting, if any field is incorrect, I show some error message and populate the correct fields using $_POST.
All the fields get its previous values except this radioButtonList. I need to set checked the value of the radioButtonList which was selected before submit, but I can't find how to do it.
Create $model = new Store(); in your action, pass it to view and use $model variable instead Store::model(). This should help.
UPD: You need to use the same $model after validation.
You can use
CHtml::radioButtonList(string $name, string $select, array $data, array $htmlOptions=array ( ));
In Your case it will be
CHtml::radioButtonList('product',$_POST[product],array(CODE1 => TEXT1,CODE2 => TEXT2,CODE3 => TEXT3));
Finally, I got a solution. (not an elegant one, but it works)
From the view:
Store::model()->product = $_POST["Store"]["product"];
Right before display the radioButtonList

Yii CHtml::ajaxSubmitButton always submits data in POST method

I came across this problem when I used CHtml::ajaxSubmitButton to submit some of the data as a GET method. No Matter what i try, It always submits data as a POST method.
Anything that I might be doing wrong? Here's my code.
echo CHtml::beginForm(array('shoppingCart/addItem','GET'));
echo CHtml::textField('qty', 1, array('size' => 3));
echo CHtml::hiddenField('product_id', $model->product_id); echo CHtml::ajaxSubmitButton('submit',
array('shoppingCart/addItem'),
array(
'type'=>'GET',
//'data' => array()),
));
echo CHtml::endForm();
Regarding to this document the value of type will be overwritten.
public static function ajaxSubmitButton($label,$url,$ajaxOptions=array(),$htmlOptions=array())
{
$ajaxOptions['type']='POST';
$htmlOptions['type']='submit';
return self::ajaxButton($label,$url,$ajaxOptions,$htmlOptions);
}
So there is no way to use GET as long as you do not touch the core function.
That is by design. You can use CHtml::ajaxButton instead:
CHtml::ajaxButton('My Label','controller/action',array(
'type'=>'GET',
), array(
'type'=>'submit',
);

get back a variable from ajax to view

i am new to php,jquery and to programming itself.I have a model called Appoinments .I am calling an ajax query from a text field to a controller action 'appoinments/loaddates'.The ajax query executed correctly. But from the ajax action i want to get some array of variables to the view.Can somebody help me please how to do it?
my view
echo CHtml::activehiddenField($model,'doctor',array(
'onchange'=>CHtml::ajax(array(
'type'=>'POST',
'url'=> CController::createUrl('loadDates'),
'update'=>'#nick_result',
'dataType'=>'json' ,
'select'=>"js:function(event, ui) {
$('#nick_result').val(ui.item.x);
}"
))
));
<div id="nick_result">
</div>
my controller
$arr[] = array(
'x'=>$x, //this I need in view
'y'=>$y,
'z'=>$z,
'p'=>$q,
);
echo CJSON::encode($arr);
how to get the $x value as a variable in php as it is used by the datepicker in my form. Any help appreciated.One thing to notice is that I just need a variable to get updated, not a model attribute. I just want to get that outside of the javascript. Thanks in advance.
To achieve this you need to use ajax callback in your view. For example:
echo CHtml::activehiddenField($model, 'doctor', array(
'onchange'=>CHtml::ajax(array(
'type'=>'POST',
'url'=> CController::createUrl('loadDates'),
'update'=>'#nick_result',
'dataType'=>'json',
'select'=>"js:function(event, ui) { $('#nick_result').val(ui.item.x); }"
'success'=>'js:function(data){
//do here what you want to do. Your json encoded array will be passed as data
}'
))
));
I recommend to use firebug for this, in console you can see what you send to ajax action in your post/get and your ajax callback (data).

Jquery Append + PHP

I'm using Yii framework and I want call a PHP function and return a value.
This value has to be appended to a div. I try a lot of things but nothing works. This is more far that I get:
echo CHtml::ajaxSubmitButton('add one more',
CController::createUrl('cadTabelapreco/UpdateFilho'),
array('type'=>'POST',
'data'=>array('item'=>'CadTabelaprecoi',
'i'=>$i,
'complete' => 'function(){
$("#data").append($(this).html());
}',
));
I also try this:
array('type'=>'POST',
'data'=>array('item'=>'CadTabelaprecoi',
'i'=>$i,
'form'=>$form),
'complete' => 'function(ret){
$("#data").append().val(ret);
}',
));
The thing is, if I debug it with Firebug, I see the response and its right, but I can't append the value to the div. How to do that?
echo CHtml::ajaxSubmitButton('add one more',
CController::createUrl('cadTabelapreco/UpdateFilho'),
array('type'=>'POST',
'data'=>array('item'=>'CadTabelaprecoi',
'i'=>$i,
'success' => 'function(data){//pass data to success function
$("#data").empty();//clear div cause if without you'll stack data in your div
$("#data").append(data);//append your url return
}',
));
In your Controller :
public function actionUpdateFilho{
/**
* Do what you need here
*/
echo $result;//form variable string with result wich you want to output(in HTml format wich you need)
}
It's working great for me. Hope this was helpfull.
The problem was the javascript. jQuery's val() returns the value of a input element. If you want to append whatever html or text was returned from the server, the last line should read:
$("#data").append(ret);

Write the values of the ajaxoptions array dynamically using Yii

I'm calling CHtml::ajaxlink like so:
<?php echo CHtml::ajaxLink('Add to a list',
$this->createUrl('itemList/ajaxadditem'),
array(
'onclick'=>'$("#addToListDialog").dialog("open"); return false;',
'type'=>'POST',
'update'=>'#addToListDialog',
'data' => 'js:{"product_id" : $("#productID").val()}'
),
array('id'=>'showAddToListDialog'));
?>
I don't know how to write the values of the AJAX options array dynamically. I'm using a workaround to get the value using JavaScript, $("#productID").val() and a hidden field.
I want to write something like:
'data' => 'js:{"product_id" : "$model->product_id"}'
But "$model->product_id" is entered as a literal string.
Can anyone give me a way to do this? My method won't actually solve the problem since I need to write this AJAX link multiple times on the fly.
Assuming your $model instance is available, you should be able to append it dynamically like below:
'data' => "js:{'product_id' : '{$model->product_id}'}"

Categories