kevindng
(Long Dhb)
August 31, 2011, 2:04am
1
Hi all,
I have a trouble when to send data from view to controller with Chtml::ajax
My View:
<div class="row">
<?php
echo CHtml::activeTextField($tr,'name',array('id'=>'txtname');
echo Chtml::button('New',array(
'onclick'=>"hello();"
));
?>
</div>
<script language="javascript" type="text/javascript">
function hello()
{
var name = $('#txtname').val();
<?php
echo Chtml::ajax(array(
'url'=>array('user/new'),
'type'=>'get',
'data'=>"",
'success'=>"function(string){
$('#show').html(string);
}"
));
?>
}
</script>
My controller
public function actionNew()
{
var_dump($_GET);
}
But i don’t know how to put valuable on “data” in ajax options.
Thank
bettor
(Live Webscore)
August 31, 2011, 9:20am
2
‘data’=>name, -> is this not working
gdang
(Gianluca Pericoli)
August 31, 2011, 9:49am
3
<div class="row">
<?php
echo CHtml::textField('txtname','');
echo Chtml::button('New',array(
'onclick'=>"hello();"
));
?>
</div>
<script language="javascript" type="text/javascript">
function hello()
{
var name = $('#txtname').val();
<?php
echo Chtml::ajax(array(
'url'=>array('corporation/asd'),
'type'=>'get',
'data'=>'js:$("#txtname").val()',
'success'=>"function(string){
alert(string);
}"
));
?>
}
</script>
this???
bettor
(Live Webscore)
August 31, 2011, 10:03am
5
I would advise to keep custom jquery separate from yii
gdang
(Gianluca Pericoli)
August 31, 2011, 11:35am
6
'data'=>'js:$("#txtname").val()',
works, I tried it, maybe you can use serialize()
drylko
(Pawel Drylo)
August 31, 2011, 1:18pm
8
Hi,
‘data’ could be a valid js call prefixed with ‘js:’ (this call should return a collection or a query string), or a query string, or even an array.
When you do not set ‘data’ and set ‘type’ property the CHtml class will set ‘data’ for you with serialized values of your form.
But it is not the case in your example. You have no form.
Remember, that your ‘success’ callback you also should prefix with ‘js:’ (you don’t have to with CHtml::ajax, but it’s a good practice, because later it is encoded with CJavaScript::encode method).
The whole would look like this:
<?php
echo Chtml::ajax(array(
'url'=>array('corporation/asd'),
'type'=>'get',
'data'=>'js:{name:$("#txtname").val()}',
'success'=>"js:function(string){
alert(string);
}"
));
?>
this is going to send an ajax request to /corporation/asd.html?name=test (if you use path for your urlFormat)
digimanpk
(Umairkhi)
August 16, 2012, 5:14am
9
I wanted to use a drop down box with ajax for each row in CGridView and this thread saved my life.
This is the code for anyone needs it.
array(
'type' => 'raw',
'header' => 'Status',
'name'=>'status',
'value' => 'CHtml::dropDownList("status-".$data->detailID,$data->status,Yii::app()->params["deliverystatus"], array("ajax"=>array("type"=>"POST", "data" => "js:{status:jQuery(this).val()}", "url" => "ordersdelivery/update?id=".$data->detailID)))',
'htmlOptions'=>array('style'=>'text-align:center;min-width:30px;')
),