Cannot use javascript variable in JQuery-escaped model->method()

I am trying to get a value from the database based on a user input but it is not working properly. if I alert the order variable, I get back the value expected. When I hard code a value as in ‘12345’ everything works as expected. If I try to use the order javascript variable in my getOrder method, I get back “undefined”




Yii::app()->clientScript->registerScript('test',"

$('#myButton').click(function(){

   order = $('#userInput').val();

   //alert(order);


   var data = '".CJSON::encode($model->getOrder('12345')."';

   d = JSON.parse(data);

   //alert(d.customer_po);


   /*

      other code to add user feedback will go here

   */

}

",CClientScript::POS_READY);



I would like this to be an AJAX call to somehow read the user input from userInput and use the $model->getOrder() method to retrieve a customer po number (along witho other data from the database and use this information in my JQuery to provide feedback to the user (ie: adding a row to a table showing the selected data).

Any ideas why I cannot escape the string and use the order variable in my method? Is there another way to do this?

Try removing the quotes around the CJSON::encode() call. Assuming that the data is a string, the encode call should add the necessary quotes.

You’re missing the closing bracket:


CJSON::encode($model->getOrder('12345'))

Thank you Keith and bennouna for a quick response. Well, the missing parenthesis was a typo, and I tried removing the quotes but that did not work. When I manually use an order number in the function, it works fine - I just cannot use the variable.

This works:


var data = '".CJSON::encode($model->getOrder('123456'))."';

These do not:




var data = '".CJSON::encode($model->getOrder('"+order.toString()+"'))."';

var data = '".CJSON::encode($model->getOrder("+order+"))."';



As you can see, I event tried casting the variable to a string, just in case. Any other ideas?

Hmm I see what you want to achieve. You can’t, like that, since PHP is parsed server-side, and JavaScript on client side. So technically, your JavaScript variable is only available when the Yii has already processed the view.

You can achieve the result you want through Ajax (you pass your order variable to a simple action that echoes back the JSON array).

PS Oh I see you’re already talking about an Ajax call in your first post. I’m confused, where’s the Ajax call?

Okay, I understand your point and realize AJAX is needed for this. Although I have many years experience with PHP and JavaScript, I am a newbie to the AJAX/JSON world. So, is there a way I can use AJAX to accomplish this in Yii by perhaps calling a controller action somehow that would return the data I need so it can be used in my jQuery script? How can I go about to accomplish my goal?

The goal is that a user can dynamically add order information to a table by inputting an order number. This will call up a function to query my database, get some additional data, and generate the new populated row.

Is there an example of a script somewhere that does something similar so I can have a looks at it?

Any advise would be greatly appreciated. Thank in advance!

Easy. Here’s an example:

[list=1]

[*]Controller


public function actionYourAction($order) {

    $model = new YourModel;

    echo CJSON::encode($model->getOrder($order));

}

[*]View


Yii::app()->clientScript->registerScript('test',"

$('#myButton').click(function(){

    order = $('#userInput').val();

    $.get('" . $this->createUrl("yourAction") . "', {'order': order}, function(data) {

        alert(data.someObjectAttribute);

        alert(data.someOtherObjectAttribute);

    }, 'json');

}

",CClientScript::POS_READY);

[/list]

WOW! You’re awesome! Thank you for the help. It looks like it is exactly what I need. I will definitely give it a try. I appreciate all of your time and your quick responses!

:)