【发布时间】:2011-02-13 07:29:56
【问题描述】:
我是 CodeIgniter 的新手,刚刚发现使用 GET 方法通过 URL 传递变量的困难(例如 domain.com/page.php?var1=1&var2=2)。
我收集到一种方法是在 URI 段中传递变量,但还没有完全弄清楚如何做到这一点,因为它似乎创建了在控制器中具有名为特定 URI 段的函数的期望? ???
无论如何,我决定不使用 GET,而是通过使用隐藏输入字段中的变量调整提交按钮(伪装成链接)来使用 POST。我创建了以下似乎工作正常的解决方案,但我想知道我是否在正确的轨道上,或者是否有更简单的方法通过 CodeIgniter 中的链接传递变量?
我在 application/libraries/ 中创建了以下类
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_variables {
function variables_via_link($action, $link_text, $style, $link_data) {
$attributes = array('style' => 'margin:0; padding:0; display: inline;');
echo form_open($action, $attributes);
$attributes = array('class' => $style, 'name' => 'link');
echo form_submit($attributes, $link_text);
foreach ($link_data as $key => $value){
echo form_hidden($key, $value);
}
echo form_close();
}
}
?>
使用以下 CSS:
/*
SUBMIT BUTTON AS LINK
adapted from thread: http://forums.digitalpoint.com/showthread.php?t=403667
Cross browser support (apparently).
*/
.submit_as_link {
background: transparent;
border-top: 0;
border-right: 0;
border-bottom: 1px solid #00F;
border-left: 0;
color: #00F;
display: inline;
margin: 0;
padding: 0;
cursor: hand /* Added to show hand when hovering */
}
*:first-child+html .submit_as_link { /* hack needed for IE 7 */
border-bottom: 0;
text-decoration: underline;
}
* html .submit_as_link { /* hack needed for IE 5/6 */
border-bottom: 0;
text-decoration: underline;
}
然后在 VIEW 中使用以下代码创建链接:
<?php
$link = new C_variables;
$link_data=array('var1' => 1, 'var2' => 2);
$link ->variables_via_link('destination_page', 'here is a link!',
'submit_as_link', $link_data);
?>
感谢您的帮助...
【问题讨论】:
标签: url codeigniter variables get