By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

JSON help

P: n/a
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
Jan 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Dominic Myers wrote:
[snip]

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...?

If you think the FF DOM implementation is bad, try IE.

You have to serialise the data into a textual JSON representation, there
isn't a built in way.

Dojo has stuff that can help you with this.

--
Ian Collins.
Jan 26 '06 #2

P: n/a
>
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.

Jan 26 '06 #3

P: n/a
On 2006-01-26, Dominic Myers <an***********@gmail.com> wrote:
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...?


if you're trying to send the data you want eval's opposite number
toSource.

put the data you want into the object and do

myXMLHttp.send(myObject.tosource())

or something like it. check it out carefully... i could be worng.

Bye.
Jasen
Jan 26 '06 #4

P: n/a
VK

Dominic Myers wrote:
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 :-(


Did you get the JSON library itself? <http://www.json.org>

What is the client-side data structure at the point where you are
currently stopped?

Jan 26 '06 #5

P: n/a
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 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.