简体   繁体   English

通过href传递变量?

[英]Pass a variable by href?

I've read a few resources and figured out the gist of doing this, but I'm having a hard time with it. 我已经阅读了一些资源,并弄清楚了这样做的要旨,但是我很难做到这一点。

I want to pass a var to '<a href="documents/edit">' so I tried 我想将'<a href="documents/edit">'传递给'<a href="documents/edit">'所以我尝试了
'<a href="documents/edit?var=<?php echo $this->request->post("doc_id")?>">' , '<a href="documents/edit?var=<?php echo $this->request->post("doc_id")?>">'
but it breaks my site. 但它破坏了我的网站。 I don't really know what I'm doing so could someone let me know if I did something wrong? 我真的不知道我在做什么,有人可以让我知道我做错了什么吗?

This is all taking place within an html file, fyi. 所有这些都在html文件fyi中进行。

Edit: Another thing about this is that I'm using Kendo and PHPixie. 编辑:关于这一点的另一件事是我正在使用Kendo和PHPixie。

Well, actually in PHPixie you cannot use $this->request in your views because $this doesn't represent the Controller (in which you can find the variable $request) but the View ! 好吧,实际上,在PHPixie中,您不能在视图中使用$this->request ,因为$ this并不表示Controller(可以在其中找到变量$ request),而是View!

The best way to pass the $_POST to your view is by doing this : 将$ _POST传递到视图的最好方法是执行以下操作:

public function action_myFunction()
{
    $this->view->post = $this->request->post();
    $this->view->subview = 'myView';
}

And in your view, you can do 在您看来,您可以

<div>
    <?=$post['doc_id'];?>
</div>

You can find more info on this blog : http://phpixie.jeromepasquier.com/accessing-variables-view/ 您可以在此博客上找到更多信息: http : //phpixie.jeromepasquier.com/accessing-variables-view/

By the way, it is good practice to always prepend your href and src with a starting slash. 顺便说一句,这是很好的做法,总是预先考虑您的hrefsrc与起始斜线。 Why ? 为什么呢 Because if you are on http://example.com/exa/mple and you want to go to http://example.com/doc_id you will need to tell the browser that you start the url from the host. 因为如果您位于http://example.com/exa/mple并且想转到http://example.com/doc_id ,则需要告诉浏览器您是从主机启动URL的。 Otherwise without a starting slash, you will end up on http://example.com/exa/doc_id . 否则,您将没有起始斜线,您将进入http://example.com/exa/doc_id

After searching the code I have, I found that this is the correct way to do things: 搜索完我拥有的代码后,我发现这是正确的处理方式:

<a href="<?php echo $exepath; ?>documents/edit?doc_id=document_id"></a>

Hopefully, this will help out anyone using PHPixie or Kendo in the future. 希望这将对将来使用PHPixie或Kendo的所有人有所帮助。

Views should not access POST directly. 视图不应直接访问POST。 Ever. 曾经

Passing the POST to your view will work for sure, but violates the main concept of the MVC. 将POST传递到您的视图肯定可以工作,但是违反了MVC的主要概念。

Doing this: 这样做:

<?=$post['doc_id'];?>

Will throw an error if the POST var "doc_id" is not present in the $post array because there is no error checking and PHPixie works in strict mode. 如果$ post数组中不存在POST var“ doc_id”,则将引发错误,因为没有错误检查并且PHPixie在严格模式下工作。

The BEST way to assign a variable that will be used later on the view like this: 分配变量的最佳方法,该变量将在以后的视图中使用,如下所示:

public function action_myFunction()
{
    $this->view->doc_id = $this->request->post('doc_id');  //will return NULL if not present, checking logic should be done before if needed
    $this->view->subview = 'myView';
}

And in your view: 在您看来:

<div>
    <?= $doc_id ?>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM