Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating buttons with links using CakePHP's HTML/Form Helpers

Tags:

cakephp

I would like to convert a link into a button using CakePHP Helpers.

With $this->Html->link() I am able to use an array() to include 'action' => 'view' . $user['User']['id'], but I am not sure how to include this when using $this->Form->button() instead.

Using $this->Html->link():

$this->Html->link('Click me', array(
    'controller' => 'users',
    'action' => 'view' . $user['User']['id']));

My Solutions

My solutions do not allow me to add 'action' => 'view' . $user['User']['id']

Using $this->Form->button():

echo $this->Form->button('Click me', array(
    'type' => 'button',
    'onclick' => 'location.href=\'/rentmyride/users/index/\';',
    ));

Using <input> tag:

<input type="button" class="btn btn-primary" value="Click me" 
    onclick="location.href='http://www.domain.com';">

Using <button> tag:

<button class="btn btn-success" onclick="location.href='http://www.domain.com';">
    Click me
</button>
like image 475
jsstrn Avatar asked Dec 23 '13 09:12

jsstrn


People also ask

How do you make buttons go to links in HTML?

Using button tag inside <a> tag: This method create a button inside anchor tag. The anchor tag redirect the web page into the given location. Adding styles as button to a link: This method create a simple anchor tag link and then apply some CSS property to makes it like a button.

Can buttons use href?

HTML buttons cannot have href attribute if they are created using button <button> </button> HTML tags. However, you can use href attribute if you create the buttons using link <a> </a> HTML tags.


2 Answers

HTML5 buttons have a formaction attribute for this use. Of course this works only in modern browsers

$this->Form->button(
    'Click me', 
    array(
        'formaction' => Router::url(
            array('controller' => 'users','action' => 'view' . $user['User']['id'])
         )
    )
);
like image 185
arilia Avatar answered Sep 30 '22 02:09

arilia


if you are using cakephp 2.X and above please use this line of code for link in button:

<button onclick="window.location.href='<?php echo Router::url(array('controller'=>'Users', 'action'=>'admin_index'))?>'">;Go Back</button>;

I hope this is works for you

like image 21
Badru Avatar answered Oct 03 '22 02:10

Badru