user2492854
user2492854

Reputation: 401

Yii CHtml::ajaxLink not working

In my Yii application I need to create a ajax link for a controller action. But when I click the link it is remaining in the same page.

What may be the error? My code snippet.

<?php echo CHtml::ajaxLink(
    $text = 'Click me', 
    $url = Yii::app()->createUrl('consumerRequest/manage' )



    ); ?>

I am unable to trace the source of error, is it in the logic itself or syntax ? anybody kindly help me.

Upvotes: 0

Views: 3048

Answers (5)

user2492854
user2492854

Reputation: 401

Ya I found the solution , I had to use div tag properly , the code snippet is correct

Upvotes: 0

Hearaman
Hearaman

Reputation: 8726

CHtml::ajaxLink() will make a asynchronous call to the target page. So, your page does not move to anywhere.

If you are looking to process the data which is coming from ajaxLink(). Try like this

    <?php
    echo CHtml::ajaxLink(
            'Click me', 
            Yii::app()->createUrl('consumerRequest/manage'),
            array
            (
                'success' => 'js:function(data)'
                . '{'
                . 'alert(data);'
                . '//Do what ever you want here'
                . '}'
            )

    );
    ?>

You can make the direct like as bellow

    <a href="<?=Yii::app()->createAbsoluteUrl('consumerRequest/manage');?>">Link</a>

Upvotes: 0

Marcos Navarro
Marcos Navarro

Reputation: 3

Try:

<?php echo CHtml::ajaxLink(
    $text = 'Click me', 
    $url = Yii::app()->createUrl('consumerRequest/manage'),//ajaxURL
    array(),
    array( //htmlOptions
        'href' => Yii::app()->createUrl('consumerRequest/manage'),//targetURL
    )
); ?>

or:

<?php echo CHtml::ajaxLink(
    $text = 'Click me', 
    $url = Yii::app()->createUrl('consumerRequest/manage'),//ajaxURL
    array(
        'data' => array(),
        'type'      => 'POST',
        'success'   => 'js:function(html){
            window.location.href = '.Yii::app()->createUrl('consumerRequest/manage').';//targetURL
         }'
    )
); ?>

Upvotes: 0

nazim
nazim

Reputation: 1549

And what are you going to do with the ajax results? Read this detail first.

CHtml::ajaxLink

Upvotes: 0

HarryFink
HarryFink

Reputation: 1010

Remove the assignments:

<?php echo CHtml::ajaxLink(
    'Click me', 
     Yii::app()->createUrl('consumerRequest/manage' )
); ?>

Upvotes: 1

Related Questions