get activeDropDownList value

my index.php view:


<?php  echo CHtml::activeDropDownList($teclado, 'idteclado', CHtml::listData(Teclado::model()->findAll(), 'idteclado', 'TecladoName'),

                    array(

                        'ajax' => array(

                        'type'=>'POST', //request type

                        'url'=>CController::createUrl('index'), //url to call.

                       

                        'update'=>'#conteudo', //selector to update

                        'data'=>array('teclado'=>'js: $(this).val()'),

                        

                        ))); 


<?php  $this->renderPartial('_listaritmos', 

                                      array('ritmos'=>$ritmos)

                                 );

My _listaritmos view:


 <?php echo CHtml::link('<img title="Buy" src="' . Yii::app()->request->baseUrl . '/images/buy.png" alt="Buy" />', $this->createUrl('buy', array('idritmo' => $ritmo->idritmo,'teclado'=><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?))); ?>

How to get the selected value of my activeDropDownList in _listaritmos link???

Hello. I wonder if it’s possible.

If I understand your code well, you just want to change the link in your image depending on the dropdown value.

Now, you’re doing:

[list=1]

[*]An ajax call upon dropdown change

[*]That ajax call reloads the view (index)

[*]That view renders partially another view (_listaritmos)

[/list]

I believe that is complex if what I said is what you want to achieve. In that case, you should do it only with jQuery without renderPartial, just to change dynamically the image target link depending on the dropdown value, with a little trick, like this:


<?php

Yii::app()->clientScript->registerScript("customScript", "

    staticUrl = '" . Yii::app()->createUrl("buy", array("idritmo" => $ritmo->idritmo)) . "';


    $('#" . CHtml:activeId($teclado, "idteclado") . "').change(function() {

        $('#buyImage').attr('href', staticUrl + '&teclado=' + $(this).val());

    });

");


echo CHtml::activeDropDownList($teclado, 'idteclado', CHtml::listData(Teclado::model()->findAll(), 'idteclado', 'TecladoName'));

?>

<a href="#" id="buyImage" title="Buy"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/buy.png" alt="Buy" /></a>

No.I believe is more simple.What i want to do is to send the value of the selected value of the combo by the link where it is “????”

An ajax call and another view rendering is simpler than DOM manipulation?

yes thanks i understand your code and works.The only problem i get now is the url format

"Unable to resolve the request "buy/idritmo/2&teclado=2"

Hmmm… ok I wrote the jQuery code in standard GET syntax.

Can you tell what the output of your normal output is for the following code?


echo Yii::app()->createUrl('buy', array('idritmo' => 2, 'teclado' => 2));

I changed:


Yii::app()->createUrl("buy", array("idritmo" => $ritmo->idritmo))

to


$this->createUrl("buy", array("idritmo" => $ritmo->idritmo))

and yes when i change combo it changes correctly the link.

The problem is that the ajax request of the activeDropDownList stopped working…

here is the firebug error:

Undefined variable: teclado

Let me say it again:

Can you tell what the output of your normal output is for the following code?


echo Yii::app()->createUrl('buy', array('idritmo' => 2, 'teclado' => 2));

Example output:

buy/idritmo/2[color="#FF0000"]…here what do you have?….[/color]

/StyleStore/buy/idritmo/2/teclado/2

Ok, so a slight adaptation:


<?php

Yii::app()->clientScript->registerScript("customScript", "

    staticUrl = '" . Yii::app()->createUrl("buy", array("idritmo" => $ritmo->idritmo)) . "';


    $('#" . CHtml:activeId($teclado, "idteclado") . "').change(function() {

        $('#buyImage').attr('href', staticUrl + '/teclado/' + $(this).val());

    });

");


echo CHtml::activeDropDownList($teclado, 'idteclado', CHtml::listData(Teclado::model()->findAll(), 'idteclado', 'TecladoName'));

?>

<a href="#" id="buyImage" title="Buy"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/buy.png" alt="Buy" /></a>

yes but i have the same ajax request error

And if you run your url without the ajax call? Doesn’t it give you the same error?

Edit: What ajax request? My answer doesn’t include any.

As you see in the first post the activeDropDownList post to the same index action.If i remove the registerScript it works.

But as you can see, in the code I gave you there’s no ajax call at all. Could you post your final code?

Yes i know,i dont understand too.Final Code??

Hmm it’ll be probably my last post in this thread today :)

I’m asking you please to post the codes that give you the error. Till then, happy testing!

You dont need to be rude ,im trying to understand what you want to help me .

Here it is the answer from firebug and the index action:


public function actionIndex()

	{

            Yii::import('application.modules.admin.models.*');

                $ultimaEncomenda=null;

                $teclado=new Teclado;

                

		if (!Yii::app()->user->isGuest)

                {

                 $ultimaEncomenda=Encomenda::model()->findBySql(

                           "select * from encomenda where idencomenda=(select max(idencomenda) from encomenda where customer=:customer)",

                           array(":customer"=>Yii::app()->user->id));   

                }

                

                

                $cat=new CDbCriteria();

                $cat->select="*";

                $cat->order="name ASC";

                

                $ritmos = new CDbCriteria();

		$ritmos->select = "*";

                $ritmos->alias="r";

                $ritmos->join="inner join ritmo_teclado on (r.idritmo=ritmo_teclado.idritmo)";

                if (isset($_POST['teclado'])) 

                  {

                    

                    $ritmos->condition="idteclado=".$_GET['teclado'];

                 

                

                  }

                  else{

                      

                      $firstRecord = Yii::app()->db->createCommand('SELECT * FROM teclado')->queryScalar();

                      $ritmos->condition="idteclado=".$firstRecord;

                  }

                 

                if (isset($_GET['letra']))

                    $ritmos->condition="artist like '".$_GET['letra']."%'";

                if (isset($_GET['categoria']))

                    $ritmos->condition="categoria=".$_GET['categoria'];

		$ritmos->order = "artist ASC";

                 if(!Yii::app()->request->isAjaxRequest)

                 {

		      $this->render('index',array('categorias'=>Categoria::model()->findAll($cat),

                                            'ritmos'=>Ritmo::model()->findAll($ritmos),

                                            'encomenda'=>$ultimaEncomenda,

                                            'teclado'=>$teclado));

                 } else { 

                     $this->renderPartial('_listaritmos', 

                                      array('ritmos'=>Ritmo::model()->findAll($ritmos)

                                 ));

                }

	}

I’m really sorry that you find what I’ve said rude. I promise I won’t again.

From your url, and your debug code, I can tell that what you have is not the code I’ve suggested above. So maybe you’ll want to post your codes (including view(s)) that gave you these errors?

My index.php:


<?php $this->pageTitle = Yii::app()->name; ?>

<div style="height:270px;">

    <div id="new" style="float:left;width:600px">

	   <?php /* $this->widget('ext.JCarousel.JCarousel', array(

    'dataProvider' => $dataProvider,

    'thumbUrl' => '"/galleria/thumbs/thumb_".$data->file_name',

    'imageUrl' => '"/galleria/".$data->file_name',

    'target' => 'big-gallery-item',

)); */ ?>

	</div>

    <div style="float: right;height: 270px;width: 178px;">

        <div id="cart">

		  <div style="width:150px;margin-left:3px;">

                <?php 

                 if ($encomenda != null and Yii::app()->session['encomenda']!=null):

                   echo CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/cart.png','cart'), $this->createUrl('encomenda/index',array('encomenda'=>Yii::app()->session['encomenda'])), array('style' => 'margin-left:15px;float:right;'));

                 endif; ?>

                <p>Produtos:<?php echo ($encomenda != null and Yii::app()->session['encomenda']!=null) ? $encomenda->quantity : 0 ?></p>

                <p>Preço:<?php echo ($encomenda != null and Yii::app()->session['encomenda']!=null) ? $encomenda->totalValue : 0 ?> €</p>


                <div align="center" style="width:150px;" class="Button1">

                    <?php echo ($encomenda != null and Yii::app()->session['encomenda']!=null) ? CHtml::link('Finalizar Encomenda', 

                            $this->createUrl('finalizar',array('encomenda'=>$encomenda->idencomenda)),

                            array('onmouseover'=>'$(this).css("color","white")',

                                  'onmouseout'=>'$(this).css("color","black")')) :

                            'Finalizar Encomenda' 

                    ?></div>

            </div>

        </div>

        <div align="center" style="color: white;margin: 10px;font-weight: bold;">

        <div>

            <p>Seleccione o Teclado</p>

            <p><?php  echo CHtml::activeDropDownList($teclado, 'idteclado', CHtml::listData(Teclado::model()->findAll(), 'idteclado', 'TecladoName')

                    ,

                    array(

                        'ajax' => array(

                        'type'=>'GET', //request type

                        'url'=>CController::createUrl('index'),               

                        'update'=>'#conteudo', //selector to update

                        'data'=>array('teclado'=>'js: $(this).val()'),

                        

                        ))

                      ); 

                     

            

            

            ?></p></div></div>

    </div>

</div>


<h1 style="background-color:white;"><i>Choose By Artist</i></h1>


<div id="listadiv"><ul id="lista">

        <li><?php echo CHtml::link('A', $this->createUrl('index', array('letra' => 'a'))); ?></li>

        <li><?php echo CHtml::link('B', $this->createUrl('index', array('letra' => 'b'))); ?></li>

        <li><?php echo CHtml::link('C', $this->createUrl('index', array('letra' => 'c'))); ?></li>

        <li><?php echo CHtml::link('D', $this->createUrl('index', array('letra' => 'd'))); ?></li>

        <li><?php echo CHtml::link('E', $this->createUrl('index', array('letra' => 'e'))); ?></li>

        <li><?php echo CHtml::link('F', $this->createUrl('index', array('letra' => 'f'))); ?></li>

        <li><?php echo CHtml::link('G', $this->createUrl('index', array('letra' => 'g'))); ?></li>

        <li><?php echo CHtml::link('H', $this->createUrl('index', array('letra' => 'h'))); ?></li>

        <li><?php echo CHtml::link('I', $this->createUrl('index', array('letra' => 'i'))); ?></li>

        <li><?php echo CHtml::link('K', $this->createUrl('index', array('letra' => 'k'))); ?></li>

        <li><?php echo CHtml::link('J', $this->createUrl('index', array('letra' => 'j'))); ?></li>

        <li><?php echo CHtml::link('L', $this->createUrl('index', array('letra' => 'l'))); ?></li>

        <li><?php echo CHtml::link('M', $this->createUrl('index', array('letra' => 'm'))); ?></li>

        <li><?php echo CHtml::link('N', $this->createUrl('index', array('letra' => 'n'))); ?></li>

        <li><?php echo CHtml::link('O', $this->createUrl('index', array('letra' => 'o'))); ?></li>

        <li><?php echo CHtml::link('P', $this->createUrl('index', array('letra' => 'p'))); ?></li>

        <li><?php echo CHtml::link('Q', $this->createUrl('index', array('letra' => 'q'))); ?></li>

        <li><?php echo CHtml::link('R', $this->createUrl('index', array('letra' => 'r'))); ?></li>

        <li><?php echo CHtml::link('S', $this->createUrl('index', array('letra' => 's'))); ?></li>

        <li><?php echo CHtml::link('T', $this->createUrl('index', array('letra' => 't'))); ?></li>

        <li><?php echo CHtml::link('U', $this->createUrl('index', array('letra' => 'u'))); ?></li>

        <li><?php echo CHtml::link('V', $this->createUrl('index', array('letra' => 'v'))); ?></li>

        <li><?php echo CHtml::link('W', $this->createUrl('index', array('letra' => 'w'))); ?></li>

        <li><?php echo CHtml::link('X', $this->createUrl('index', array('letra' => 'x'))); ?></li>

        <li><?php echo CHtml::link('Z', $this->createUrl('index', array('letra' => 'z'))); ?></li>

    </ul></div>

<div id="gmenu">

    <h6><?php echo CHtml::link('All', $this->createUrl('index')); ?></h6><br>

    <?php foreach ($categorias as $categoria): ?>


        <h6> <?php echo CHtml::link($categoria->name, $this->createUrl('index', array('categoria' => $categoria->idcategoria))); ?></h6><br>

    <?php endforeach; ?>

</div>

<div id="conteudo">

    <!--Lista de Ritmos -->

    <?php  $this->renderPartial('_listaritmos', 

                                      array('ritmos'=>$ritmos,'teclado'=>$teclado)

                                 );

     ?>




</div>

My _listaritmos:


<table id="listaritmos"  border="0">

        <tr class="tableheader"><th width="280px">Artist</th><th width="280px">Song</th><th width="5px">Demo</th><th width="5px">YouTube</th><th width="5px">Price</th> <?php if (!Yii::app()->user->isGuest): ?><th width="5px">Buy</th> <?php endif ?></tr>

        <?php foreach ($ritmos as $ritmo) { ?>

            <tr>

                <td><?php echo $ritmo->artist; ?></td>

                <td><?php echo $ritmo->title ?></td>

                <td><div align="center">

                        <?php if ($ritmo->mp3 != ''): echo CHtml::ajaxlink('<img title="Play Demo" src="' . Yii::app()->request->baseUrl . '/images/Button-Play-icon.png" alt="Play" />', $this->createUrl('playMp3'), array('update' => '#you', 'data' => array('mp3Lnk' => $ritmo->mp3), 'type' => 'POST'));

                        endif; ?>

                    </div></td>

                <td>

                    <div align="center">

                        <?php

                        if ($ritmo->youtubeLnk != ''):

                            echo CHtml::ajaxLink('<img title="YouTube" src="' . Yii::app()->request->baseUrl . '/images/youtube-icon.png" alt="youtube-icon.png" />', $this->createUrl('displayYouTube'), array('update' => '#you', 'data' => array('youtubeLnk' => $ritmo->youtubeLnk), 'type' => 'POST'));

                        endif;

                        ?>  

                    </div>

                </td>

                <td><div align="center"><?php echo $ritmo->price; ?>&euro;</div></td>

                <?php if (!Yii::app()->user->isGuest): ?>

                    <td><div align="center">

              <?php

               Yii::app()->clientScript->registerScript("customScript", "

                     staticUrl = '" . Yii::app()->createUrl("buy", array("idritmo" => $ritmo->idritmo)) . "';

                         $('#" . CHtml::activeId($teclado, 'idteclado') . "').change(function() {

                            $('#buyImage').attr('href', staticUrl + '/teclado/' + $(this).val());

                      });");

                echo CHtml::link('<img title="Buy" src="' . Yii::app()->request->baseUrl . '/images/buy.png" alt="Buy" />',$this->createUrl("buy", array("idritmo" => $ritmo->idritmo,"teclado"=>1)),array('id'=>'buyImage')); ?>

                        </div></td> 

    <?php endif ?>

            </tr>	

<?php } ?>




    </table>



My action index:


public function actionIndex()

	{

            Yii::import('application.modules.admin.models.*');

                $ultimaEncomenda=null;

                $teclado=new Teclado;

                

		if (!Yii::app()->user->isGuest)

                {

                 $ultimaEncomenda=Encomenda::model()->findBySql(

                           "select * from encomenda where idencomenda=(select max(idencomenda) from encomenda where customer=:customer)",

                           array(":customer"=>Yii::app()->user->id));   

                }

                

                

                $cat=new CDbCriteria();

                $cat->select="*";

                $cat->order="name ASC";

                

                $ritmos = new CDbCriteria();

		$ritmos->select = "*";

                $ritmos->alias="r";

                $ritmos->join="inner join ritmo_teclado on (r.idritmo=ritmo_teclado.idritmo)";

                if (isset($_POST['teclado'])) 

                  {

                    

                    $ritmos->condition="idteclado=".$_GET['teclado'];

                 

                

                  }

                  else{

                      

                      $firstRecord = Yii::app()->db->createCommand('SELECT * FROM teclado')->queryScalar();

                      $ritmos->condition="idteclado=".$firstRecord;

                  }

                 

                if (isset($_GET['letra']))

                    $ritmos->condition="artist like '".$_GET['letra']."%'";

                if (isset($_GET['categoria']))

                    $ritmos->condition="categoria=".$_GET['categoria'];

		$ritmos->order = "artist ASC";

                 if(!Yii::app()->request->isAjaxRequest)

                 {

		      $this->render('index',array('categorias'=>Categoria::model()->findAll($cat),

                                            'ritmos'=>Ritmo::model()->findAll($ritmos),

                                            'encomenda'=>$ultimaEncomenda,

                                            'teclado'=>$teclado));

                 } else { 

                     $this->renderPartial('_listaritmos', 

                                      array('ritmos'=>Ritmo::model()->findAll($ritmos)

                                 ));

                }

	}

        

I believe that the ajax request of the activeDropDownList that occurs when has a change are confusing the change function of the registerScript…

Ok :) There was a misunderstanding here since my first post.

[list=1]

[*]In your “index” view, remove completely the ajax call from the dropdown, you won’t need it anymore, like this


<p><?php echo CHtml::activeDropDownList($teclado, 'idteclado', CHtml::listData(Teclado::model()->findAll(), 'idteclado', 'TecladoName')); ?></p></div></div>

[*]In your "_listaritmos" partial view, change the corresponding code with:


<?php

Yii::app()->clientScript->registerScript("customScript", "

    staticUrl = '" . Yii::app()->createUrl("buy", array("idritmo" => $ritmo->idritmo)) . "';


    $('#" . CHtml:activeId($teclado, "idteclado") . "').change(function() {

        $('#buyImage').attr('href', staticUrl + '/teclado/' + $(this).val());

    });

"); ?>

<a href="#" id="buyImage" title="Buy"><img src="<?php echo Yii::app()->request->baseUrl; ?>/images/buy.png" alt="Buy" /></a>



[*]When it works, we’ll see that we can change the controller as well.

[/list]