Author Topic: [SOLVED] need help passing array from php to ajax function....  (Read 845 times)

0 Members and 1 Guest are viewing this topic.

Offline meomike2000Topic starter

  • Enthusiast
  • Gender: Male
    • View Profile
i have a php file that gets the row id # of selected rows from database table.

and an ajax function that will seperate out the row ids.

where can i find some examples of this in action so that i can try to understand how it should work. i have never tried to pass an array, only strings with the print $string.

any help would be great.......

thanks mike

Offline corbin

  • Guru
  • Freak!
  • *
  • Gender: Male
    • View Profile
Re: need help passing array from php to ajax function....
« Reply #1 on: June 30, 2009, 12:52:22 PM »
Look into JSON or XML.  (JSON is usually easier in my opinion.)
Why doesn't anyone ever say hi, hey, or whad up world?

Offline meomike2000Topic starter

  • Enthusiast
  • Gender: Male
    • View Profile
Re: need help passing array from php to ajax function....
« Reply #2 on: June 30, 2009, 01:45:06 PM »
ok i will, thanks.....

Offline meomike2000Topic starter

  • Enthusiast
  • Gender: Male
    • View Profile
Re: need help passing array from php to ajax function....
« Reply #3 on: June 30, 2009, 04:34:53 PM »
 maybe it is just me but i dont seem to understand how to do this....

can somebody please point me to a simple working example of how i can do something like this......

in the php file i get this
Code: [Select]
$userid = $_GET['uid'];
$index = 0;
$ids = array();

$query = null;
//open connection to db.
$connection = mysql_connect($host, $user, $pass)
or die ('unable to connect!');
//select  database to use.
mysql_select_db($db) or die ('unable to select database!');
$query = "select * from friends where myid = '" . $userid . "' and status = 'accepted'";
$result = mysql_query($query);
$query = null;
header("Content-Type: plain/text");
while ($row = mysql_fetch_row($result))

$ids[$index] = $row[0];
}
mysql_free_result($result);
//close database
mysql_close($connection);

print $ids;

now to handle this in my ajax i have this segment....
and yes i know that it would only display the word test if it was working.......
in my getfriends.js
Code: [Select]
this.parseYamlResult = function(str) {
var arr = [];
var res = [];
var pat = /(\S+): (\S+)\n/g;
while (arr = pat.exec(str)) {
res[arr[1]] = arr[2];

}
return res;
};

this.sort = function(str) {
var self = getfriends;
var newdiv = null;
var ids = [];
var div = self.Div;
ids = self.parseYamlResult(str);
while (var i = 0; i < ids.length; i++) {


newdiv = document.createElement('div');
newdiv.id = 'test';
newdiv.appendChild(document.createTextNode('test'));
div.appendChild(newdiv);
}
};

Offline meomike2000Topic starter

  • Enthusiast
  • Gender: Male
    • View Profile
Re: need help passing array from php to ajax function....
« Reply #4 on: June 30, 2009, 04:42:47 PM »
update
changed the while to a for and removed the yamlparser. now i am gettine some results........
Code: [Select]
this.sort = function(str) {
var self = getfriends;
var newdiv = null;
var ids = [];
var div = self.Div;

for (var i = 0; i < str.length; i++) {


newdiv = document.createElement('div');
newdiv.id = 'test';
newdiv.appendChild(document.createTextNode('test'));
div.appendChild(newdiv);
}
};

Offline meomike2000Topic starter

  • Enthusiast
  • Gender: Male
    • View Profile
Re: need help passing array from php to ajax function....
« Reply #5 on: June 30, 2009, 04:49:46 PM »
update
well i made some more changes
and now it prints Array out on the screen....
please help....

Code: [Select]
this.sort = function(str) {
var self = getfriends;
var newdiv = null;
var ids = [];
ids = str; 
var div = self.Div;
for (var i = 0; i < str.length;) {
var id = str[i];
i++
newdiv = document.createElement('div');
newdiv.id = 'test';
newdiv.appendChild(document.createTextNode(id));
div.appendChild(newdiv);
}
};

Offline meomike2000Topic starter

  • Enthusiast
  • Gender: Male
    • View Profile
Re: need help passing array from php to ajax function....
« Reply #6 on: June 30, 2009, 06:11:48 PM »
update....
ok i can now get the info that i want, the id#

the problem i am now having is that for some reason all my double digit id#s get separated like so...... 15 18 3 2 14 would read out 1   5  1  8  3  2  1  4....

can anybody please help.......

here is my two codes...
php code
Code: [Select]

<?php
$userid 
$_GET['uid'];
$index 0;
$ids = array();

$query null;
//open connection to db.
$connection mysql_connect($host$user$pass)
or die (
'unable to connect!');
//select  database to use.
mysql_select_db($db) or die ('unable to select database!');
$query "select friendid, friendname from friends where myid = '" $userid "' and status = 'accepted'";
$result mysql_query($query);
$query null;
header("Content-Type: plain/text");
while (
$row mysql_fetch_row($result))
{  
$id $row[0];
print $ids[$index] = $id;
$index ++;
}
mysql_free_result($result);
//close database
mysql_close($connection);

?>


and the part of my ajax that handles this is

Code: [Select]

this.sort = function(str) {
var self = getfriends;
var newdiv = null;
var id = '';
var ids = str;
var div = self.Div;
for (var i = 0; i < ids.length; i++) {
var id = ids[i];

newdiv = document.createElement('div');
newdiv.id = 'test';
newdiv.appendChild(document.createTextNode(id));
div.appendChild(newdiv);
}
};


Offline meomike2000Topic starter

  • Enthusiast
  • Gender: Male
    • View Profile
Re: need help passing array from php to ajax function....
« Reply #7 on: June 30, 2009, 06:36:47 PM »
update

ok... not sure it is right but it seems to be working for me anyhow.....

my php file i added a | to the end of id for a seperator......

Code: [Select]
<?php
$userid 
$_GET['uid'];
$index 0;
$ids = array();

$query null;
//open connection to db.
$connection mysql_connect($host$user$pass)
or die (
'unable to connect!');
//select  database to use.
mysql_select_db($db) or die ('unable to select database!');
$query "select friendid, friendname from friends where myid = '" $userid "' and status = 'accepted'";
$result mysql_query($query);
$query null;
header("Content-Type: plain/text");
while (
$row mysql_fetch_row($result))
{  
$id $row[0];
print $ids[$index] = $id."|";
$index ++;
}
mysql_free_result($result);
//close database
mysql_close($connection);

?>


and form my ajax i have this..... (and it all prints out correctly now)

Code: [Select]
this.sort = function(str) {
var self = getfriends;
var br = self.br;
var newdiv = null;
var id = 0;
var ids = str.split('|');
var div = self.Div;
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
newdiv = document.createElement('div');
newdiv.id = 'test';
newdiv.appendChild(document.createTextNode(id));
newdiv.appendChild(self.br);
div.appendChild(newdiv);

}
};

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.