Send a value to the controller from Ajax

Hi everyone!

I need to send a value from a dropdownlist to the controller in order to update a record.

So far I putt this in the view:




<div class="form_row">

<?php  

//

// THE DROPDOWN LIST FILLED WITH AUTHORS

//

echo CHtml::dropDownList('new_author_id', null, Chtml::listData(Author::model()->findAll(), 'id', 'lastname'));

//

// THE AJAX LINK

// 

echo CHtml::ajaxLink("Add author",

	CController::createUrl('publications/AddAuthorAndUpdateAjax'),

	array('update' => '#ajax_content', 'data' => array(

		'publication_id' => $_GET['id'], // The current publication

		'new_author_id' => '' // HERE I NEED TO GET THE SELECTED VALUE FROM THE DROPDOWN LIST

		)

	)

);

?>

</div>



And in the controller:




public function actionAddAuthorAndUpdateAjax(){

	$author_id = $_GET['new_author_id']; // HERE I WANT TO GET THE VALUE!!!

	$publication_id = $_GET['publication_id'];

	//

	$record = new PublicationAuthor;

	$record->author_id = $author_id;

	$record->publication_id = $publication_id;

	$record->save();

	//

	$publication = Publication::model()->findByPk($publication_id);

	//

	$this->renderPartial('_related_authors', array('authors' => $publication->getAuthors()));

}



The thing is if I hardcode the value in the ajax statement it works fine, but I need to get the selected value from the dropdown list.

With jQuery, I would get that value with -> $("select#new_author_id").val();

How I should pass the value???

Thanks in advance!!!

Sorry I posted this twice, I reloaded the browser accidentally so, if an admin could delete one it would be great…

Sorry again.

Hello Luciano.

Try the following at the ajaxLink:


'new_author_id'=>'js:function() { return $("#new_author_id").val(); }'

Ok, Gerhat…just let me say that you are my new God…

Thanks, man…it works like a charm… :)

Yeah! It works great!!!

But with a small detail…it works great just once… <_<

After that, you need to reload the page…

Well known bug, I guess -> http://code.google.com/p/yii/issues/detail?id=38 and http://www.yiiframework.com/forum/index.php?/topic/1710-ajaxsubmitbutton-works-only-once/

I have to get ride of this functionality…sad…

Anyways…thanks for that piece of code, it will be very usefull tu me! :)