jquery $.get and data return

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

jquery $.get and data return

Post by webstyler »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi

I need to send id to script and return array..

I have my first_page.php

Here I have try this

[syntax="javascript"]$.get('log.php',{id:'4'},function(data){
alert("Data Loaded: " + data);
});
log.php receive id..

Now, i need GET into first_page.php a result as js array..

How I must prepare this data in log.php and how I can get this on first page ?

Txx


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

I think the word you're looking for is JSON. json.org maintains a list of available JSON libraries (scroll to bottom of page). I used Services_JSON for this purpose (after ripping it out of the class to a simple function).
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

Post by webstyler »

uhm..

jquery support json : http://docs.jquery.com/Ajax#.24.getJSON ... llback_.29

but .. I don't get value :oops:

with


$.get('log.php',{id:"5"},function(data){alert("data" + data);})

I get a print of value that I need

I need only to know how to use this value as js array in my first page
:)
tx
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Yes, jquery supports json, however:


1) You're not using $.getJSON()...
2) Are you returning a JSON string?
3) What is the exact output of log.php?
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

Post by webstyler »

I have been try JSON


My code in the first_page.php

Code: Select all


var mycontacts = new Array();
$.getJSON("log.php",{id: "5"}, function(ps){
for (var i = 0; i < ps.length; i++)
{
mycontacts[i] = new Array('ps[0].idd', 'ps[1].email', 'ps[2].nome','ps[3].idg');
}
});
log.php return this syntax:

Code: Select all

[{idc: 22, email: ex@amp.le, nome: Myname, idg: 3},{idc: 22, email: ex@amp.le, nome: Myname, idg: 3},{idc: 22, email: ex@amp.le, nome: Myname, idg: 3}]
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

webstyler wrote:I have been try JSON


My code in the first_page.php

Code: Select all


var mycontacts = new Array();
$.getJSON("log.php",{id: "5"}, function(ps){
for (var i = 0; i < ps.length; i++)
{
mycontacts[i] = new Array('ps[0].idd', 'ps[1].email', 'ps[2].nome','ps[3].idg');
}
});
log.php return this syntax:

Code: Select all

[{idc: 22, email: ex@amp.le, nome: Myname, idg: 3},{idc: 22, email: ex@amp.le, nome: Myname, idg: 3},{idc: 22, email: ex@amp.le, nome: Myname, idg: 3}]
Remove the quotes in the new array statement...

Code: Select all

new Array(ps[0].idd, ps[1].email, ps[2].nome, ps[3].idg);
You probably also want to keep the ps index the same, as you're taking info from three different sets of data.

Code: Select all

new Array(ps[i].idd, ps[i].email, ps[i].nome, ps[i].idg);
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

Post by webstyler »

thx, but seem not return data :(

I have also correct idc with idd

nothing to do

after loop

Code: Select all

			
for (var i = 0; i < ps.length; i++)
                    {
	  mycontacts[i] = new Array(ps[i].idd, ps[i].email, ps[i].nome, ps[i].idg);
	    }
How can I check if and want value returned ?

Code: Select all

alert(mycontacts.lenght);
is ok ?
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Use firefox and install the firebug extension.

Then you can do:

Code: Select all

console.log(variable);
//or
console.dir(array_or_object);
You will find out exactly what is contained within.

<var>.length should also work...
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You might also find this a valuable reference (I do!):

http://www.visualjquery.com/1.1.1.html
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

Post by webstyler »

nickvd wrote:Use firefox and install the firebug extension.

Then you can do:

Code: Select all

console.log(variable);
//or
console.dir(array_or_object);
You will find out exactly what is contained within.
Now I try tx
nickvd wrote: <var>.length should also work...
If I alert .lenght I return "undefined"
:(
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

Post by webstyler »

nickvd wrote:Use firefox and install the firebug extension.

Then you can do:

Code: Select all

console.log(variable);
//or
console.dir(array_or_object);
You will find out exactly what is contained within.
output is

[ ]

:(

After [] I see GET and correct result..
I don't understand why result is not pass to my var

Think this correct and must return data, no ?

Code: Select all

$.getJSON("log.php",{id: "5"}, function(ps){
for (var i = 0; i < ps.length; i++)
                    {
          mycontacts[i] = new Array(ps[i].idd, ps[i].email, ps[i].nome, ps[i].idg);
            } 
});
console.log(mycontacts); 
webstyler
Forum Commoner
Posts: 85
Joined: Sat Feb 14, 2004 2:16 am

Post by webstyler »

I have try this:

Code: Select all

var mycontacts = new Array();
$.getJSON("log.php",{id: 5}, function(ps){ 
for (var u = 0; u < ps.length; u++) 
            { 
             mycontacts[u] = new Array(ps[u].idd, ps[u].email, ps[u].nome, ps[u].idg); 
              console.log(mycontacts[u][1]);
            } 
}); 

console.log(mycontacts); 

the first console output is OK
the second, out of function and $.getJSON result nothing: [ ]

?

seems that var is empty after function.. but is global

Nb. var is defined before

help pls :(
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

webstyler wrote:I have try this:

Code: Select all

var mycontacts = new Array();
$.getJSON("log.php",{id: 5}, function(ps){ 
for (var u = 0; u < ps.length; u++) 
            { 
             mycontacts[u] = new Array(ps[u].idd, ps[u].email, ps[u].nome, ps[u].idg); 
              console.log(mycontacts[u][1]);
            } 
}); 

console.log(mycontacts); 

the first console output is OK
the second, out of function and $.getJSON result nothing: [ ]

?

seems that var is empty after function.. but is global

Nb. var is defined before

help pls :(
I'm going to assume that you didn't read the json.org page that was posted earlier...

If your php file is returning a json string you will need to either use a json parser (read the site) or use eval() to turn the json data into the javascript variable it represents...
Post Reply