SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing javascript object to another page

    Hey guys,

    Another fun problem!

    I have a fairly complex javascript object - containing strings and arrays - which I need to pass along to a second PHP page. Is there a way to pass this object along via POST to be picked apart by the receiving PHP script?

    I could dynamically create a form, populate it with the contents of the object, then pass it along via submit(), but frankly that's an awful pain. I was hoping there's a simpler way.

    Any suggestions? Thank again -

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Go get Danny Goodman's newest book! He's got some nice routines for serializing (& de-serializing) JS objects.
    I'd post it here but they'd probably arrest me. Maybe someone else has a solution (might be zipped here, no time to check).
    ::: certified wild guess :::

  3. #3
    ********* Janitor levik's Avatar
    Join Date
    Jan 2003
    Location
    New York
    Posts
    885
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you need your server-side code to be able to examine the object, or simply for it to be available on the client when the next page loads?

    If it's the latter, I suggest you use frames. For example, set the object into a JS variable of the frameset page, then on the next page, ask it back from the parent.

    As long as your frameset and frame are under the same hostname, you should not have any issues.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found it (in the zipped examples).
    Code:
    /* objectsArraysStrings.js */
    /*
         Example File From "JavaScript and DHTML Cookbook"
         Published by O'Reilly & Associates
         Copyright 2003 Danny Goodman
    */
    
    function object2String(obj) {
        var val, output = "";
        if (obj) {    
            output += "{";
            for (var i in obj) {
                val = obj[i];
                switch (typeof val) {
                    case ("object"):
                        if (val[0]) {
                            output += i + ":" + array2String(val) + ",";
                        } else {
                            output += i + ":" + object2String(val) + ",";
                        }
                        break;
                    case ("string"):
                        output += i + ":'" + escape(val) + "',";
                        break;
                    default:
                        output += i + ":" + val + ",";
                }
            }
            output = output.substring(0, output.length-1) + "}";
        }
        return output;
    }
    
    function array2String(array) {
        var output = "";
        if (array) {
            output += "[";
            for (var i in array) {
                val = array[i];
                switch (typeof val) {
                    case ("object"):
                        if (val[0]) {
                            output += array2String(val) + ",";
                        } else {
                            output += object2String(val) + ",";
                        }
                        break;
                    case ("string"):
                        output += "'" + escape(val) + "',";
                        break;
                    default:
                        output += val + ",";
                }
            }
            output = output.substring(0, output.length-1) + "]";
        }
        return output;
    }
    
    
    function string2Object(string) {
        eval("var result = " + string);
        return result;
    }
    
    function string2Array(string) {
        eval("var result = " + string);
        return result;
    }
    Presumably, insert into a hidden field for your PHP.

    Plenty of issues - with using frames at all, if you aren't already.
    ::: certified wild guess :::

  5. #5
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help guys! I'll give that code a good look through.

    And thanks for the frames tip, adios - now that I think about it, that has an awful lot of potential...


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
  •