Jump to:

23370 Posts in 18160 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » Variables only displaying when page is refreshed

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: 334 Views
  • neilos
    Avatar
    Community Member
    19 Posts

    Variables only displaying when page is refreshed Link to this post

    Hi,

    I asked this in a different section, but haven't received a reply in almost a week.
    http://www.silverstripe.org/data-model-questions/show/21293

    Since asking, I have modified it a bit, but still only works when refreshing the page.

    I am trying to get a variable from various google location ads to change the phone number depending on the ?number=xxx from the ad.

    Currently the number displays a default value unless the page is refreshed, then the variable is displayed.
    I need it to display the number without the need for a refresh.

    Is there a better way of doing this that will display the variable number without the need for a page refresh?

    In page.ss

    <% if phonenumber %>$phonenumber <% else %>0800 xxx xxx<% end_if %>


    In page.php

    public function Phonenumber() {
    if (!isset($_SESSION['number']))
    {
    $phonenumber = $_GET['number'];
    $_SESSION['number'] = $phonenumber;
    }
    return strip_tags (Session::get ('number'));
    }


    Any advice or pointers are massively appreciated.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Variables only displaying when page is refreshed Link to this post

    You might be better writing that function as

    $number = Session::get('number');

    if($get = $this->request->getVar('number')) {
    $number = $get;
    Session::set('number', $get);
    }

    return Convert::raw2xml($number);

  • neilos
    Avatar
    Community Member
    19 Posts

    Re: Variables only displaying when page is refreshed Link to this post

    Willr, thanks. That has fixed all my woes.
    Except the admin section is white screening with no error being thrown.

    I created a different page type and put this function on there instead of page.php and the admin section is accessible again.

    One more question, is it possible to use a field in the CMS that can be used as the variable?
    I.E.

    $fields->addFieldToTab('Root.Content.Main', new TextField('StaticNumber'), 'Content');

    is this possible?

    334 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.