0

I have a CHTML:ajax function that does some AJAX stuff on a select dropdown - I simply want to do something which says..

"on change, grab the selected value & pass that as param childID in the URL"

This should then display the following in the url section of the

CHTML::ajax function:-
 'url' => 'isAjax=1&childID=5134156'

I've tried to append the variable selected onto the url but it doesn't work - can anyone see what I'm doing wrong

jQuery(function($) {
    $('#child-form select[name="Child[user_id]"]').bind('change', function(e){
  var selected = this.value;
  console.log('selected : '+selected ); // outputs an ID to the console.

   <?php echo CHtml::ajax(array(
                  'url' => '?isAjax=1&childID='+selected,
                  'type' => 'post',
                  'update' => '#parents-sidebar',
       // rest of the ajax function (quite long...)
Insane Skull
  • 9,220
  • 9
  • 44
  • 63
Zabs
  • 13,852
  • 45
  • 173
  • 297

2 Answers2

0

You can't take a value extracted from the dom using javascript and inject it directly into PHP code.

From the PHP.net documentation:

Since Javascript is (usually) a client-side technology, and PHP is (usually) a server-side technology, and since HTTP is a "stateless" protocol, the two languages cannot directly share variables.

CHtml::ajax() is primarily a shortcut for generating javascript code. So the easy solution would just be to write your javascript manually. That will allow you to use your selected variable.

Note: You might try Taron Saribekyan's solution, posted in the comments. The idea is that the javascript expression ('...+selected') will be printed by PHP as a string, and thus be evaluated by javascript. In theory, this should work.

ethan
  • 985
  • 6
  • 10
0

That is obvious. You have defined a javascript variable and you are using it in your php code! Everything inside <?php ?> block, will interpret on the server and before javascript. So, I think you should use normal jquery ajax method in your case. Something like this:

$.ajax({
   "url": <?php echo Yii::app()->baseUrl.'/controller/action' ?>'?isAjax=1&childID='+selected,
    'type' => 'post',
     ...
})
hamed
  • 7,939
  • 15
  • 60
  • 114