Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How do you pass arrays between html pages?

    I have an array that has user accounts and need to pass it to another page. How can i do this? My teacher said we can only use html and javascript.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,193
    Thanks
    23
    Thanked 605 Times in 604 Posts
    The big question here is "how did you get the user accounts data?". Using only html and javascript I'm guessing it was user entered via a form. Forms pass data, and that's what I would do. Of course not being able to use PHP makes recovering the data on the next page hard.

    P.S.
    just pass the data, don't use arrays.
    Look at this article for help: http://www.irt.org/articles/js063/

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    That code Sunfighter pointed you to works, but it's really ugly an clumsy if you need to pass lots of form fields. It's silly to have to go look through the entire query string each time you need a value from a name=value pair. Do it one time, instead, and create a lookup table.

    I prefer to do this:
    Code:
    <script type="text/javascript">
    var qs = []; // where we will put the fields with their values
    
    if ( location.search.length > 2 )
    {
        var pairs = location.search.substring(1).split("&");
        for ( var p = 0; p < pairs.length; ++p )
        {
            var pair = pairs[p].split("=");
            qs[ pair[0] ] = decodeURIComponent( pair[1] );
        }
    }
    Now that qs[] variable will hold a key/value "dictionary" of all the name=value pairs that were passed from the prior page's <form> in the query string.

    So, for example, you could say
    Code:
        var name = qs["name"];
    and then use the value obtained as needed in the second page.

    CAUTION: That code will *NOT* handle any multiple occurrences of the same name. It's easy to adapt it to do so, if you need to do so.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Dec 2011
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Is there a way to write to a specific node on a xml file? For example lets say i have the following xml file:
    Code:
    <Database>
         <employee>
         <username>joe</username>
         <password>joe111</password>
         </employee>
      <employee>
         <username>sam222</username>
         <password>sam222</password>
         </employee>
      <employee>
         <username>tom33</username>
         <password>tom333</password>
         </employee>
    </Database>
    I want to let employees to change their password. So lets say if sam wants to change his password. I want only his password to change. I try to do it but it doesnt work.


    Nevermind i got it to work
    Last edited by phenom01; 12-18-2011 at 08:48 PM.

  • #5
    New Coder
    Join Date
    Dec 2011
    Location
    Earth if you want to know
    Posts
    27
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by phenom01 View Post
    I have an array that has user accounts and need to pass it to another page. How can i do this? My teacher said we can only use html and javascript.
    Hi phenom,

    I suggest using function toString() to convert an Array to a String (then you can pass this page to page without any worry, right? It becomes a String), then you can use .split() to get it back as an Array.

    Example:

    Code:
    //Set an Array contains username and password, then make it a string
    var userArray = ["Username", "Password"];
    var userString = userArray.toString();
    
    //Send info to process.html
    location.href = "process.html?info=" + userString;
    Then in file process.html:

    Code:
    //location.search.split["="] will return ["info", userString];
    var userString = location.search.split["="][1];
    
    //Now you get the original userArray = ["Username", "Password"];
    var userArray = userString.split(",");
    I hope this will help.

    Tim_

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,343
    Thanks
    11
    Thanked 589 Times in 570 Posts
    just use localStorage or setCookie and getCookie to share your info.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,506
    Thanks
    77
    Thanked 4,378 Times in 4,343 Posts
    For the life of me, I don't understand the point of this.

    You can *NOT* share that XML file--or *ANY* data--among multiple users when you are using ONLY client-side (in the browser) coding.

    You *MUST* do that with server-side coding. Period. End of statement.

    You can add Joe to the XML data on Joe's machine, if you use local storage.

    You can add Mary to the XML data on Mary's machine, if you use local storage.

    But Joe will never see that Mary has been added and Mary will never see that Joe has been added.

    If this is for a school project, okay. Fine. It's 100% silly and a waste of time, but if you must do it, you must. But there is no real world application for it. At all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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