SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2011
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using PHP variable set from localStorage value as Refresh rate

    This will probably be an easy one for all of you, but it's sure got me stumped. I am working on a mobile optimized site and decided to write a very simple slideshow for pictures instead of going with already made options as I tried many and all were sluggish on my old mobile phone. What I've created works exactly how I want except for one thing. I decided to create a control panel which allows the user to set the rate of the slide show. I store that info in localStorage using simply...
    $seconds = $_GET['seconds'];
    echo "<script>localStorage.setItem('seconds',\"$seconds\");</script>";

    That seems to work fine as I can see the data being stored and I can echo it out as well.

    ----- Below I get attempt to get the seconds the user wants and store it into a variable. This seems to almost work as if I tell it to echo $second, it'll display correctly what's in localStorage.----
    $seconds == "<script>document.write(localStorage.getItem('seconds'));</script>";

    --- This isn't really important for you, but here I tell it that if there is no stored value in localStorage (meaning the user hasn't set a value yet) to just use 10 seconds as default for the slideshow. This works mostly, meaning it works without the other stuff I listed here, but if there "is" a value in the localStorage it skips this as it should, but that's where the problem occurs as you'll see in the next part.-----
    if ($seconds == NULL){$seconds = 10;}

    --- This is where the problem seems to be. There IS a value for $seconds, but it won't work here. I've tried simply setting "$seconds = 5" just to make sure I "can" use a variable here, and THAT works fine, but if 5 is stored in the localStorage and recorded to $seconds, then it won't work. It'll echo out the correct value of $seconds, but it has no effect in setting the refresh rate, yet it knows it's not NULL as it skips the above IF and thus does NOT set for the default 10 seconds and instead it doesn't refresh at all.
    if ( $refresh != NULL && $image != 15 ) {header("refresh: $seconds; gallery.php?tgp=$tgp&image=$nextimage&refresh=1");}

    This is a little more information than you need, but I wanted to make sure not to miss something that might be important. If I manually enter a value for $seconds like "5" then that works fine. If I manually set $seconds to 5 then that works too. If I echo $seconds, it'll tell me 5. But when it uses that value from the localStorage in the statement above it does nothing. It knows it's not NULL so it knows there is a value there, but it does nothing.

    I'm guessing it's not treating what is stored in $seconds as a number? I should also point out that I am NOT a programmer. I know nothing about programming and must look up every little thing. Takes me hours to write something simple. My code sucks I'm sure, but my goal is to get my code to "work" and that's allowed me to build some fairly impressive sites over the years which have stood the test of time--that's the goal for me. So please make your comments in easy to understand English and avoid calling me stupid too many times.

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <script>document.write(localStorage.getItem('seconds'));</script> - Will write everything locally on the client browser, it does not send the information to the server.
    $seconds - is a server variable available only on the server. What you can do is use AJAX to send the localStorage.getItem('seconds') to the server.

  3. #3
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    $seconds == "<script>document.write(localStorage.getItem('seconds'));</script>";
    Is there a reason you're using comparison operators (==) instead of an assignment operator (=)?
    Keith
    Freelance web developer
    http://www.duvalltech.com/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •