Problem With Chtml::ajax And Objects

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

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/

[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.

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);




Wow, great.

I was testing this code getting

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);