Shahid Karimi
Shahid Karimi

Reputation: 4357

yii Ajax link not working

I put a Ajax link using the following code:

echo chtml::ajaxLink('GO', 'http://localhost/index.php?r=user/delete', array('method'=>'POST'));

But, regardless of giving the second parameter as URL i,e 'http://localhost/index.php?r=user/delete'. It generates link with the current URL in the browser not the URL I just specified. What is the issue? How could I create AJAX link? Google several hours but can't solve the issue. Any kind of help is highly appreciated.

Upvotes: 0

Views: 7640

Answers (3)

artuu
artuu

Reputation: 31

I had the same issue(or maybe similar). I've used renderPartial to load view and later in that view i was using ajaxLink and it was not working.

What i have found, that when using renderPartial, there was no jquery script for ajax action. What you have to do is to add 4th argument(true) in renderPartial function to generate jquery script.

See the documentation: http://www.yiiframework.com/doc/api/1.1/CController/#renderPartial-detail

Hope it helps and saves time to figure it out.

Upvotes: 0

bool.dev
bool.dev

Reputation: 17478

First of all, you should always try and create normalized urls.
But i think your doubt lies in the # that is generated/appended. If you go and check the source of yii ajaxLink you'll see this:

public static function ajaxLink($text,$url,$ajaxOptions=array(),$htmlOptions=array())
{
    if(!isset($htmlOptions['href']))
        $htmlOptions['href']='#';
    $ajaxOptions['url']=$url;
    $htmlOptions['ajax']=$ajaxOptions;
    self::clientChange('click',$htmlOptions);
    return self::tag('a',$htmlOptions,$text);
}

so if you don't set the href property of the a tag in the htmloptions array, the # will be appended.

You should also understand that yii uses jquery, so if you check out the source of the page, you'll see at the bottom, how jquery is used to carry out an ajax request, your actual url that is called will also be seen in that script. So the third option/parameter in ajaxLink is for options for jquery's ajax function. You can create better ajax links using this option.
Regardless of where(which controller) your url points to in your project, the action associated with that url will be called.

So anyway, you can modify your code like this if you want the url to be shown and not a # :

echo CHtml::ajaxLink('GO', 'http://localhost/index.php?r=user/delete',
 array('type'=>POST), //there are various other options for jquery ajax
 array('href'=>'http://localhost/index.php?r=user/delete'));

To make better ajax links i would suggest going through jquery's ajax documentation. There is an option for a success function, that you can use to let the user know that the operation was completed.

Hope this helps, don't hesitate to leave comments if i haven't answered your question completely.

Upvotes: 3

stratosgear
stratosgear

Reputation: 962

Have you tried:

echo CHtml::ajaxLink('GO', array('/user/delete'), array('method'=>'POST'));

as the ajaxLink documentation suggests...? Look also at the normalizeUrl method.

Using these methods, which in turn are using createUrl, is usually better since it will take care to create a valid url for your site.

Upvotes: 1

Related Questions