SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    earth
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remembering dropdown value

    Hello say I have a drop down that says "select 1 (default), a, b, c". When a user clicks a, he is redirected to another page which also has the same dropdown. How can I make it (using javascript) so that "a" is selected instead of "select 1 (default)"? Thanks.

  2. #2
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I do not believe this is possible sorry expert_21. you will need to use a language such as PHP to transfer the data to the new page. Javascript does not save its state across multiple page refreshes.

    Regards,
    Jordan

  3. #3
    SitePoint Evangelist borna's Avatar
    Join Date
    Jun 2006
    Location
    Iran, Tehran
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is that because of
    Javascript does not save its state across multiple page refreshes.
    or because "1" was selected as default value.
    -1/2=999?

  4. #4
    I ♥ PHP
    Join Date
    Jul 2003
    Location
    Melbourne, Australia
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by borna
    is that because of or because "1" was selected as default value.
    It is because to change the value of the select box, the browser needs to know what value was selected on the previous page. Javascript exists only for a single page, not across an entire site. You need to tell the second page which value was selected in the first, and the only way to do so is to use a server side language. Once you have that value you can modify the value of the secone select box.

    Regards,
    Jordan

  5. #5
    SitePoint Evangelist borna's Avatar
    Join Date
    Jun 2006
    Location
    Iran, Tehran
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, that was what I thinking for. J2EE and java programming are huge in content and almost handy to work with. (instead of php)
    -1/2=999?

  6. #6
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using only JavaScript seems contrary to the use of a form for this data, but you could accomplish this, with varying levels of reliability, with either cookies or using the window.name property as a data storage cheat.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    earth
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the replies. Yes, I can set it up so that the php calls a JS function to change the selected to a value that the user selected previously. But can you give me an example of how the JS function will be? Thanks.

  8. #8
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It just seems, since you are already going to the step of loading a second page, that a server script would be just as easy. I'd probably suggest using the window.name property here, since many people have cookies disabled. There is no real trick to it, the property simply stores a text string. Although some older browsers had some issues, the newer ones seem pretty reliable. Just assign a string value (best to avoid spaces in the string) to the window.name property with: window.name=string_value

    and read it as you would any variable, as in: alert(window.name);


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
  •