Jump to content

[SOLVED] need help passing array from php to ajax function....


meomike2000

Recommended Posts

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

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

$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

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);
	}	
};

update

changed the while to a for and removed the yamlparser. now i am gettine some results........

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);
	}	
};

update

well i made some more changes

and now it prints Array out on the screen....

please help....

 

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);
	}	
};

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


<?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

 


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);
	}	
};

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

 

<?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)

 

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);

	}	
};

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.