473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

JSON help

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
5 1676
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
>
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: George Jempty | last post by:
In my journal anyway: "JSON: Javascript Unusable by the Client" http://slashdot.org/~scriptify/journal/103074
16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
3
by: asleepatdesk | last post by:
Hi, I need some help here. When I try to eval() my AJAX returned JSON string, I continually get a javascript error "Expected )". Here's my JSON string: {"recs": }; My js function simply...
9
by: eggie5 | last post by:
eval is not working on this JSON string: {"top":} {"car":} {"man":} data = eval('(' + json + ')'); What's wrong with the json string, is it 'cause it has no root?
2
by: kungfumike | last post by:
I am currently having an issue getting json to work. The version of json,js I am using can be found here: http://www.json.org/json.js The file is innclided in standard html <script...
1
by: seth | last post by:
Hi: I'm trying to read JSON strings sent from the browser. Here is the scenario: 1. Using YUI tookit 2. sending JSON string from YUI toolkit - using the provided asyncRequest method. **I...
3
by: xhe | last post by:
I found Jason is a very handy data format for client/server communication. But I just met a problem as follows: I want to read the data replied from server in Jason format, the reply is like...
19
RMWChaos
by: RMWChaos | last post by:
Previously, I had used independent JSON lists in my code, where the lists were part of separate scripts. Because this method did not support reuse of a script without modification, I decided to...
3
Kelicula
by: Kelicula | last post by:
Hi all, I am usually a Perl programmer, I have some background in javascript and am attempting to create a Googleish selector div. Please bear with me, excuse the long introduction... Here's...
0
by: JonTwend | last post by:
I am working with the Twitter API and finding it great fun and generally easy to use. But I am having trouble getting to the data in the trends/current.json file. I am able to loop through the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.