Pass a variable by href?
-
20-12-2019 - |
Pergunta
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?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.
Edit: Another thing about this is that I'm using Kendo and PHPixie.
Solução 2
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.
Outras dicas
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 !
The best way to pass the $_POST to your view is by doing this :
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/
By the way, it is good practice to always prepend your href
and src
with a starting slash. 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. Otherwise without a starting slash, you will end up on http://example.com/exa/doc_id
.
Views should not access POST directly. Ever.
Passing the POST to your view will work for sure, but violates the main concept of the 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.
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>