Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Passing a session or paramter

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 225 Views
  • Bagzli
    Avatar
    Community Member
    31 Posts

    Passing a session or paramter Link to this post

    Hi guys, I've spent hours researching this and trying to implement various means but i'm at the dead end right now.

    I want to accomplish the following scenario:

    I want to click on a button or image, call a function and pass some sort of indicator that this specific button/image was clicked and then have that php function do something based on which indicator we pass in. Is this possible? I'm open to using sessions or params, or JS, I need to get this scenario working ASAP. On a deadline

    Any help is most appreciated!

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Passing a session or paramter Link to this post

    That is quite a broad question. For 'click something and execute code' You can use GET parameters. A basic example might look like

    <a href="home/?doawesome=1">Do it</a>

    On your page in the init() function (or index, or whatever action) you could do something like

    class Page_Controller extends ContentController {

    public function init() {
    ...

    if($this->request->getVar('doAwesome')) {
    // do the awesome, modify the current $this page.
    $this->Title = "Some new Title";
    }
    }
    }

    Note that if doAwesome is destructive (or modifies the user state) you should protect the link with the Security token to prevent CSFR.

    If you want to keep the user on the same page, look at adding a click handler on the link with jQuery and doing the link via $.get().

  • Bagzli
    Avatar
    Community Member
    31 Posts

    Re: Passing a session or paramter Link to this post

    Thank you so much! I've got it to work now, I'll show you what I did. I still need to change so that the database doesn't search for partial matches but I'll figure something out for that. But here is what it is doing:

    This is my controller, ArticleHolder.php

    class ArticleHolder extends Page {
    private static $allowed_children = array('ArticlePage');

    private static $db = array(
    'Filters' => 'Text'
    );

    public function getCMSFields() {

    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Main', new TextField('Filters'), 'Content');

    return $fields;
    }
    }
    class ArticleHolder_Controller extends Page_Controller {
    public function ValidateType(){
    if($this->request->getVar('tag')){
    $filter = $this::get()->filter('Filters:PartialMatch', $this->request->getVar('tag'))->First();
    if ($filter == NULL){
    return NULL;
    }
    else{
    return $this->PaginatedPages();
    }
    }
    else{
    return $this->PaginatedPages();
    }
    }
    public function PaginatedPages(){
    if($this->request->getVar('tag')){
    $tag = $this->request->getVar('tag');
    }
    else{
    $tag = 'News';
    }
    $paginatedItems = new PaginatedList($this->filterArticles($tag), $this->request);
    $paginatedItems->setPageLength(3);
    return $paginatedItems;
    }
    public function filterArticles($tag){
    return ArticlePage::get()->filter('category:PartialMatch', $tag)->sort('Date DESC');
    }
    }

    This is my View ArticleHolder.ss

    <% require themedCSS('ArticleHolder') %>
    <% require themedCSS('SideBar') %>
    <div>
    <div>
    <ul>
    <li id="testFilter"><a href="news/?tag=Other"><img src="assets/other/bagzli_logo.png" alt="image"/></a></li>
    </ul>
    </div>
    <div id="news">
    <% if ValidateType() %>
    <ul>
    <% loop $PaginatedPages %>
    <li>
    <div class="article">
    <h2><a href="$Link" title="Read more on &quot;{$Title}&quot;">$Title</a></h2>
    <h3>$Date</h3>
    <img class="indent" src="$Photo.link" alt="image"/>
    <p class="indent">$Teaser</p>
    </div>
    </li>
    <% end_loop %>
    </ul>
    <% include Pagination %>
    <% else %>
    <p>SORRY NO RESULTS WERE FOUND</p>
    <% end_if %>
    </div>
    <div id="sidebar">
    </div>
    <div class="clear"></div>
    </div>

    In the controller I'm checking if the passed variable belongs to the approved variable list. I'm hoping to stop any kind of SQL Injection with this.

    So i'm just clicking on an image and then i refresh the page with the new variable and new results appear. Now I don't understand what you were talking about if it is destructible, I have not heard of that before. Could you please elaborate a bit more on that?

    225 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.