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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

	}	
};

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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