Button with link to controller action

(Rulmonroy) #1


I have a register action in my site to allow the guest user to create a new account for him.

Also I have a button in my index page with a link to the register action and that’s where my problem begins. I tried with this:

<?php echo CHtml::button('Register', array('submit' => array('site/register'))); ?>

When I click the button nothing happens. I also tried with a link:

<?php echo CHtml::link('LinkText',array('site/register'),array('class'=>'btn_registro')); ?>

And it works perfectly but… I want a button !! hehe

I’d appreciate any help please. :S

(Matteo Falsitta) #2

Just use a link and style it as a button. Buttons worls properly only in forms, you should create a form with action the action you want, and put the button in the form.

A better approach is to use links, using the background you can style the link as a button, and even better.

(_Stan_) #3

For those who may be interested in implementing the same behaviour, I’m just faced the same problem and solved it with the following code:

CHtml::button('Title', array('onclick' => 'js:document.location.href="controller/action"'));

Works for me.

(Igor Savinkin) #4

(Igor Savinkin) #5

Inside the referrence in a button you add


before a controller/action. Like this:


(Shgninc) #6

Also you can use CHtml::linkButton()

(Andrea) #7

Thanks!It works, but ignore all $_GET data, you have insert it like this:

<?php echo CHtml::button('Title', array('onclick' => 'js:document.location.href="index.php?r=controller/action&id='.$model->id.'"')); ?>

istead of:

<?php echo CHtml::link('Title', array('controller/action', 'id'=>$model->id)); ?>

(F Gruber) #8

Please can you help me: :rolleyes:

How to style a link as a button in Yii?

(Pawel) #9

Bootstrap style:

    background-image: none;

    border: 1px solid transparent;

    border-radius: 4px;

    cursor: pointer;

    display: inline-block;

    font-size: 14px;

    font-weight: normal;

    line-height: 1.42857;

    margin-bottom: 0;

    padding: 6px 12px;

    text-align: center;

    vertical-align: middle;

    white-space: nowrap;

    background-color: #000;

    color: #fff;

    text-decoration: none;

Create a css class with the above and use it on link.

(Tambadiyasohel) #10

please use this below its looking like button

<?php echo CHtml::link('ExportAll',array('user/exportall'),array('class'=>'btn yellow btn-sm','style'=>'float:right')); ?>