JSON help

Discussion in 'Javascript' started by Dominic Myers, Jan 26, 2006.

  1. In the full and frank knowledge that someone will
    doubtless refer me to google and probably tell me
    to wipe my own arse I was wondering if someone
    could shed some light on the following problem for
    me?

    I have a number of select blocks that I want to
    get the data from into a PHP script on the server
    (I know, sounds very AJAXie doesn't it?). I'm
    thinking that about the best way to do this would
    be via JSON and the following structure would be
    ideal really:

    [ { 'select0' : 'CSB1030C' },
    { 'select1' : 'CSB1005C' },
    { 'select2' : 'CSB1039C' },
    { 'select3' : 'MSB1016' },
    { 'select4' : 'CSB1003C' },
    { 'select5' : 'CSB1038C' } ]

    The selects take this form:

    <select id="select0" onchange="">
    <option value="CSB1030C">abc</option>
    <option value="CSB1005C">def</option>
    <option value="CSB1039C">efg</option>
    <option value="CSB1038C">hij</option>
    etc...
    </select>
    <select id="select1" onchange="">
    <option value="CSB1030C">abc</option>
    <option value="CSB1005C">def</option>
    <option value="CSB1039C">efg</option>
    <option value="CSB1038C">hij</option>
    etc...
    </select>
    <select id="select2" onchange="">
    <option value="CSB1030C">abc</option>
    <option value="CSB1005C">def</option>
    <option value="CSB1039C">efg</option>
    <option value="CSB1038C">hij</option>
    etc...
    </select>
    etc...


    How do I get the onchange thing on each of the
    selects to grab the selected values for all of the
    selects and pass it to the server using JSON. I
    can grab the data fine using the DOM (even
    FireFox's interesting implementation of the DOM
    isn't insurmountable really), but I can't figure
    out how to encode the data. I'm reading a lot
    about eval() but also seeing that some people feel
    that it is evil...?

    Any help would be greatly appreciated... just a
    pointer or two would be cool as I feel as though
    I've been bashing my head against this for 4 days
    now :-(

    Cheers in advance,

    Dom
     
    Dominic Myers, Jan 26, 2006
    #1
    1. Advertising

  2. Dominic Myers

    impaler Guest

    >
    > How do I get the onchange thing on each of the
    > selects to grab the selected values for all of the
    > selects and pass it to the server using JSON. I
    > can grab the data fine using the DOM (even
    > FireFox's interesting implementation of the DOM
    > isn't insurmountable really), but I can't figure
    > out how to encode the data. I'm reading a lot
    > about eval() but also seeing that some people feel
    > that it is evil...?


    Let me undestand exactly what you want.
    Do you need to pass an object to the server ? Do you want to have all
    the selected options back on the server? You need no JSON for this.
    JSON stand for JavaScript Object Notation and it's often used by server
    side scripts to deliver ready to use objects for javascript not vice
    versa. Of course you can do it, but is too much overhead to deserialize
    a json format when all you have to do is pass a comma separated list of
    key:value pairs for instance.

    Build something like this:
    'select0:CSB1030C,select1:CSB1005C....
    and you can split having the comma as separator thus obtaining the
    key:value pairs and do a split on all these pairs having the colon as
    separator and there you have them.

    on the onchange event you can have a function that builds this string
    and if you use XMLHttpRequest to pass it...use it in this function but
    beware of launching multiple requests before the server response.
     
    impaler, Jan 26, 2006
    #2
    1. Advertising

  3. Dominic Myers

    Guest

    Dear Ian, impaler, Jasen and VK,
    You are all diamonds. True diamonds. I looked at sending just a text
    file but thought that that wouldn't work, but of course it could work a
    treat. Thanks muchly all you've helped me get my head around what was
    really doing it in.
    Cheers again,
    Dom
     
    , Jan 26, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    244
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    346
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    258
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    265
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    170
    David Karr
    Jun 17, 2013
Loading...

Share This Page