Codeigniter将id从控制器传递给视图 [英] Codeigniter pass a id to controller from view

查看:70
本文介绍了Codeigniter将id从控制器传递给视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用codeigniter为这个项目。我hava一个id值,其中我从控制器A传递到视图A.这个id值是锚标签之间的回声。当点击这个锚点标签时,它重定向到具有id值的另一个控制器B,并在控制器B中处理这个id值。除了使用uri类,有没有其他方法这样做?想要保持url干净。



当我从控制器A转换到视图A到控制器B时,我想到了一种附加隐藏输入元素的方法,但是我意识到这可能很乱。



这样做的任何干净的方法?

解决方案

/ p>

方法1



使用URI类除非你有很好的理由不要。



从CONTROLLER_A

  $ data [id] =(ID NUMBER); 
$ this-> load-> view(VIEW_A,$ data);

ANCHOR IN VIEW A

 < a href =<?php echo base_url()?> / controller_b / controllerfunction /<?php echo $ id?>> link< ; / a> 

IN CONTROLLER_B

  $ id = $ this-> uri-> segment(3); 



方法2



如果您想保留隐藏的内容,请使用FORM POST:

  $ data [id] =(ID NUMBER); 
$ this-> load-> view(VIEW A,$ data);

ANCHOR IN VIEW A

 < form name =myformid =myformaction =<?php echo base_url()?> / controllerB / controllerfunction /method = post> 
< input type =hiddenname =idid =idvalue =<?php echo $ id?> />
< input type =submitvalue =查看更多/>
< / form>

您也可以使用javascript通过链接提交表单,如果您:

 < a href =javascript; onclick =document.getElementById('myform')。submit();>查看更多< / a>提示:您还可以使用css通过将透明度设置为0来隐藏表单中的提交按钮;您可以使用css隐藏表单中的提交按钮。 
如果链接在表单中,您可以使用 javascript:this.submit();



OR JQUERY

 < a href =javascript; id =link>查看更多< / a> 
$('#link')。click(function(){
$('#myform')。submit();
});



IN CONTROLLERB

  $ id = $ this-> input-> post(id); 


I am using codeigniter for this project. I hava a id value in which i pass from controller A to view A. This id value is echo between an anchor tag. When this anchor tag is clicked on, it redirects to another controller B with the id value and processed this id value within controller B. Is there any other way of doing this other than using the uri class? Want to keep the url clean.

I thought of a way of appending hidden input elements when I shift from controller A to view A to controller B, but i realised it can be very messy.

Any clean ways of doing this? Thanks in advance guys!

解决方案

New to using Stackoverflow See if you could understand my layout:

METHOD 1

Use the URI Class except you have good reasons not to.

From CONTROLLER_A

$data["id"] = ("ID NUMBER");
$this->load->view("VIEW_A", $data);

ANCHOR IN VIEW A

<a href="<?php echo base_url() ?>/controller_b/controllerfunction/<?php echo $id ?>">link</a>

IN CONTROLLER_B

$id = $this->uri->segment(3);

.

METHOD 2

USE FORM POST if you want to keep things hidden:

$data["id"] = ("ID NUMBER");
$this->load->view("VIEW A", $data);

ANCHOR IN VIEW A

<form name="myform" id="myform" action="<?php echo base_url() ?>/controllerB/controllerfunction/" method="post">
<input type="hidden" name="id" id="id" value="<?php echo $id ?>" />
<input type="submit" value="See more" />
</form>

You could also use javascript to submit the form here via link if you which:

 <a href="javascript;" onclick="document.getElementById('myform').submit();"> See more</a>

Tips:You could also use css to hide submit button in the form by setting opacity to 0; If link is within the form, you could use javascript:this.submit();

OR JQUERY

<a href="javascript;" id="link">See more</a>
$('#link').click(function() {
    $('#myform').submit();
});

IN CONTROLLERB

$id = $this->input->post("id");

这篇关于Codeigniter将id从控制器传递给视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆