Page 1 of 1

jquery $.get and data return

Posted: Sat Feb 24, 2007 8:42 am
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]

Posted: Sat Feb 24, 2007 10:08 am
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).

Posted: Sat Feb 24, 2007 11:12 am
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

Posted: Sat Feb 24, 2007 11:45 am
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?

Posted: Sat Feb 24, 2007 11:51 am
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}]

Posted: Sat Feb 24, 2007 12:03 pm
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);

Posted: Sat Feb 24, 2007 12:46 pm
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 ?

Posted: Sat Feb 24, 2007 1:51 pm
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...

Posted: Sun Feb 25, 2007 12:01 am
by Kieran Huggins
You might also find this a valuable reference (I do!):

http://www.visualjquery.com/1.1.1.html

Posted: Sun Feb 25, 2007 5:04 am
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"
:(

Posted: Sun Feb 25, 2007 10:05 am
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); 

Posted: Mon Feb 26, 2007 11:32 am
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 :(

Posted: Tue Feb 27, 2007 4:34 pm
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...