Using js variable in ajax call

Is it possible to use js variables in the ajax call?


    function getArticleData()

          {

		var values = {};

		$.each($('#orders-form').serializeArray(), function(i, field) {

			values[field.name] = field.value;

		});

		

		var rowNo = values.row_no;

		var artNoField = "art_no"+rowNo;

		var artAmField = "art_amount"+rowNo;

		var art_no = values[artNoField];

		var art_amount = values[artAmField];


		<?php 

			echo CHtml::ajax(array(

                        'url'=>CController::createUrl('orders/getArticleData'), 


                        'data'=>array('rowNumber'=>'js:$(\'#row_no\').val()', 

				      'articleNumber'=>'js:$(\'#art_no\').val()', 

                                      'articleAmount'=>'js:$(\'#art_amount\').val()',

                                      ),

                        'type'=>'post',

                        'dataType'=>'json',

                        'success'=>"function(data)

                        {

                              // Update the status

                              $('#articleStatus').html(data.status);

                              if (data.error=='false')

                              {

                                    $('#art_name').val(data.art_name);

                                    $('#art_price').val(data.art_price);

                                    //$('#art_price_all').val(data.art_price_all);

                              }

 

                        } ",

                   ))?>;

                   return false;  

             } 

I want to use the art_no variable instead of the jquerry code (js:$(\’#art_no\’).val()).

Hi,

Where do you put your code - in a view inside script tags? Why not to write plain jquery then? This way you’ll have your var easily accessible.

Hi! Yes, it’s inside a view.

What exactly do you suggest? I need the php part to make some db query via an ajax call. The form field names varies (you can add new rows via javascript and the fields are named by their assignment + the current row number (art_no1, art_no2, art_no3, etc). That’s why I added the js code to get the right form field name.

Where can I use plain jquery to do the intended job?

Thx for your help sofar…!

I mean




    function getArticleData()

          {

                var values = {};

                $.each($('#orders-form').serializeArray(), function(i, field) {

                        values[field.name] = field.value;

                });

                

                var rowNo = values.row_no;

                var artNoField = "art_no"+rowNo;

                var artAmField = "art_amount"+rowNo;

                var art_no = values[artNoField];

                var art_amount = values[artAmField];

$.ajax({

    type: 'post',

    url: "<?php echo CController::createUrl('orders/getArticleData');?>",

    data: {'rowNumber': rowNo, 'articleNumber': art_no, 'articleAmount': art_amount},

    dataType: 'json',

    success: function(data){

     	// Update the status

    }

});

               	return false;  

         	} 

This worked great! Thx alot for you help!