JSON help

D

Dominic Myers

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
 
I

impaler

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

annoyingmouse

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top