Image link with $confirmMessage alert in Cakephp HTMLhelper - possible?

风格不统一 提交于 2019-12-11 12:00:02

问题


Is it possible to create an image with a link that also has a pop up alert [$confirmMessage] using the html helper in CakePHP?

This is my current text link:

$this->Html->link('Clear list', array('controller' => 'items', 'action' => 'clearlist', $model['Model']['id']), array(), 'Clear list?')

This how the image helper creates images with links:

echo $this->Html->image("recipes/6.jpg", array( "alt" => "Brownies", 'url' => array('controller' => 'recipes', 'action' => 'view', 6)));

However this allows only an htmlattributes array as the arguements for the link.

The $confirmMessage alert is not an html attribute is it?

This is the code I tried:

echo $this->Html->link($this->Html->image("clearall.png", array("alt" => "Clear list")), array('controller' => 'items', 'action' => 'clearlist', $model['Model']['id']), array(), 'Clear list?');

However this code printed the correct html for my img as text:

<img src="/img/clearall.png" alt="Clear list" />

Do I have to give up on htmlhelper in this case?


回答1:


CakePHP does do this with the Html helper and you were really close!

<?php echo $this->Html->link($this->Html->image('clearall.png', array(
                                                    'alt' => 'Clear list')
                                               ), array(
                                                    'controller' => 'items',
                                                    'action' => 'clearlist',
                                                    $model['Model']['id']
                                               ), array(
                                                    'escape' => false,
                                                    'confirm' => 'Clear list?'
                                               )); ?>

You could also have done it without the helper like so:

<a href="/items/clearlist/<?php echo $model['Model']['id']; ?>"
   onclick="return confirm(&#039;Clear list?&#039;);">
    <img src="/img/clearall.png" alt="Clear list" />
</a>

Thanks to ADmad and rtconner for showing me this in IRC.



来源:https://stackoverflow.com/questions/8794213/image-link-with-confirmmessage-alert-in-cakephp-htmlhelper-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!