Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Passing a session or paramter


Go to End


3 Posts   708 Views

Avatar
Bagzli

Community Member, 71 Posts

16 February 2014 at 10:58am

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!

Avatar
Willr

Forum Moderator, 5523 Posts

16 February 2014 at 5:37pm

Edited: 16/02/2014 5:38pm

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().

Avatar
Bagzli

Community Member, 71 Posts

16 February 2014 at 7:35pm

Edited: 17/02/2014 6:34am

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?