iaing
(Igray)
October 21, 2012, 1:34pm
1
I’m having a problem with the script below to update a field based on a dropdown selection changing.
What I want it to do is send a userid and orderid via ajax. I know how to access it, but I can’t work out how to stop CHTML::ajax from enclosing the orderid and userid in quotes so I can send the contents of the javascript variables.
$ajax = CHtml::ajax(array(
'type'=>'POST',
'url'=>CController::createUrl('user/ajax'),
'data'=> array( 'order'=> 'orderid', 'user'=>'userid'),
));
//Script for updating user from dropdown
$dropDownScript = <<<EOD
$('select[name=user_id]').change(function () {
var orderid = $(this).parent().siblings(":first").text();
var userid = $(this).val();
dataObj = { order : orderid, user : userid };
$ajax
EOD;
Yii::app()->clientScript->registerScript("dropDown", $dropDownScript, CClientScript::POS_READY);
The above generates:
jQuery.ajax({'type':'POST','url':'/site/index.php?r=user/ajax','data':{'order':'orderid','user':'userid'},'cache':false});
what I want is:
jQuery.ajax({'type':'POST','url':'/site/index.php?r=user/ajax','data':{'order':orderid,'user':userid},'cache':false});
How can I fix this?
Thanks,
Iain
alirz23
(Alirz23)
October 21, 2012, 2:07pm
2
do you ajax call thru jquery itself dont use Yii’s ajax method
EDIT:
<?php Yii::app()->clientScript->registerCoreScript("jquery"); ?>
<script type="text/javascript">
$(document).ready(function() {
$('select[name=user_id]').change(function () {
var orderid = $(this).parent().siblings(":first").text();
var userid = $(this).val();
dataObj = { order : orderid, user : userid };
$.ajax({
type: "POST",
url: "user/ajax",
data: dataObj,
success: function(data) {
$('.result').html(data);
}
});
});
});
</script>
jquery ajax apis
http://api.jquery.com/jQuery.ajax/
softark
(Softark)
October 21, 2012, 2:07pm
3
[s]
Hmm, maybe this?
dataObj = { 'order' : orderid, 'user' : userid };
[/s]
Ah, yes. alirz23 is right. It’s better to use jQuery directly in this case.
seenivasan
(Chellamnivas)
October 21, 2012, 2:42pm
4
Dear Friend
Can you please try something like this?
$ajax = CHtml::ajax(array(
'type'=>'POST',
'url'=>CController::createUrl('user/ajax'),
'data'=>'js:dataObj',
));
//Script for updating user from dropdown
$dropDownScript = <<<EOD
$('select[name=user_id]').change(function () {
var orderid = $(this).parent().siblings(":first").text();
var userid = $(this).val();
dataObj = { "order" : orderid, "user" : userid };
$ajax
EOD;
Yii::app()->clientScript->registerScript("dropDown", $dropDownScript, CClientScript::POS_READY);
alirz23
(Alirz23)
October 21, 2012, 3:24pm
6
$ajax = CHtml::ajax(array(
'type'=>'POST',
'url'=>CController::createUrl('user/ajax'),
'data'=>'js:dataObj',
));
//Script for updating user from dropdown
$dropDownScript = <<<EOD
$(‘select[name=user_id]’).change(function () {
var orderid = $(this).parent().siblings(":first").text();
var userid = $(this).val();
dataObj = { "order" : orderid, "user" : userid };
$ajax
EOD;
Yii::app()->clientScript->registerScript("dropDown", $dropDownScript, CClientScript::POS_READY);
I was testing this code getting
iaing
(Igray)
October 21, 2012, 4:54pm
7
Thanks all - I found the solution, seenivasan had it right, you need to use js:dataObject.
My final code:
$ajax = CHtml::ajax(array(
'type'=>'POST',
'url'=>CController::createUrl('order/ajax'),
'data'=> 'js:dataObj',
));
$dropDownScript = <<<EOD
$('select[name=user_id]').change(function () {
dataObj = { func: 'changeuser', order : $(this).parent().siblings(":first").text(), user : $(this).val() };
$ajax
});
EOD;
Yii::app()->clientScript->registerScript("dropDown", $dropDownScript, CClientScript::POS_READY);