Advanced Search

Results 1 to 7 of 7

Thread: Help with personalised background

  1. #1
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with personalised background

    Im a newbie at javascript, and needs help in coding my new web.

    What i need now is a code when loaded will look for a cookie, if there is a value in the cookie, it will load the value as the background image, if there is no value/no cookie exist, it will create 1 cookie with a default value of an image url.

    And at the end of the page there is a form that allow user to input image url for the cookie to remember.

    Can someone please generate the code for me, you help is most appreciated.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    Well, you could do it in PHP as well, and it would be pretty easy.

    Here's the code for that:

    Make sure your page ends with the .php extension and PHP is installed/enabled on your server, then place this at the very top of yourpage:

    PHP Code:
    <?php
    if (isset($_POST['bgimage'])) {
    $bgimage $_POST['bgimage'];
    setcookie('bgimage',$bgimage,time()+60*60*24*365);
    }
    else if (isset(
    $_COOKIE['bgimage'])) {
    $bgimage $_COOKIE['bgimage'];
    }
    else {
    $bgimage 'mydefault.jpg'//set default here
    setcookie('bgimage',$bgimage,time()+60*60*24*365);
    }
    ?>
    I set the cookie to expire in one year. Change that if you'd like...


    Now, you have the value $bgimage ready for use in setting the background on the page.

    Use the following line:
    PHP Code:
    <?=$bgimage?>
    wherever you want to have the value of the background set.
    For example:
    <body background="<?=$bgimage?>">
    Or, using CSS:
    background-image: <?=$bgimage?>;
    Etc.

    Now, for the form, use some version of this:
    PHP Code:
    <form .... action="">
    <input type="text" name="bgimage" value="<?=$bgimage?>">
    <input type="submit" value="Change Background">
    </form>
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A server-side script is definitely the preferred way to do it.
    Or, using CSS:
    background-image: <?=$bgimage?>;
    Code:
    background-image: url(<?php echo $bgimage; ?>);
    djr33, never use shorttags when designing code to run on someone else's server. There's no guarantee they'll have them enabled.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,154
    Thanks
    260
    Thanked 690 Times in 678 Posts

    Default

    Oops, on the CSS. I was writing it quickly.

    As for short tags, isn't it fairly well supported for the simple output format, when just displaying a certain value? I never use anything aside from <?php ... ?> in general, but for displaying a value, I find <?=$val?> or <?=val()?> to be a lot cleaner.

    I should have noted, though, that the proper substitute if that fails is to use echo, as in your post.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    isn't it fairly well supported for the simple output format, when just displaying a certain value?
    No. Shorttags are shorttags; there's only one option to enable all of <?, <?=, &c. In my experience, about one in four servers have it disabled.
    I should have noted, though, that the proper substitute if that fails is to use echo, as in your post.
    But that's a lot more effort than just using the full form in the first place
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have managed to figure out the code somehow. thanks anyway guys.

  7. #7
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    but the i will try the php, it might have a better result cause my code is messed up

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
  •