DotDragnet
May 24, 2012, 07:17:58 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: jQuery array (type thing)  (Read 284 times)
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« on: September 22, 2011, 12:06:44 PM »

I want to collect a series of IDs on a page and then push them through to a PHP script as a comma separated list. In a nutshell

for each row
save the userID into an array
implode/join the array values by a comma
send to php script

I have

Code:
var userids = $();
$('tr.am').each(function(i,row){
var rowid = $(row).attr('id');
var row_id = rowid.split('_');
userids = userids.add( row_id[3] );
});

// need to join userids to get 1,2,3,4,5 etc.

$.getJSON("includes/script.inc.php", {uid: userids, y: yr, m: mth}, function(data) {
etc.

With an array I would use userids.join(','); but this isn't an array as such but using the jQuery object (I found this idea/method whilst looking for something else and read it was better to use than creating an Array).

Anyone got an idea how I can take userids and get them into a comma separated values string?

TIA smile
Logged

Whatever
Hero Member
*****
Posts: 750



View Profile WWW Awards
« Reply #1 on: September 22, 2011, 12:23:27 PM »

Isn't this was what you were helping me with Sarah on my 'basic form' thread.

The form I was submitting wanted to use a comma separated values string too.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #2 on: September 22, 2011, 12:28:43 PM »

yes, I can do it in PHP, but I don't know how in jQuery as it's not an array it's an object.

Currently found a way to turn the object back to an array, not sure if that's the best solution but it's the easiest one at present.
Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 551



View Profile Awards
« Reply #3 on: September 22, 2011, 01:14:39 PM »

If you don't need anything special with $() then don't use it.

var userids = [];
...
userids.push(row_id[3]);
...
userids = userids.toString();
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #4 on: September 22, 2011, 01:23:13 PM »

Thanks Jason. No I'm not doing anything special, just collecting IDs and then creating a CSV list out of them, I just read it was the better way to do it  smile

Daft question but I presume the userids will go into the function no problem (i.e. unlike PHP where you'd have to pass it in or make it global). Sorry, I don't use JS much these days and am quite out of touch!
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #5 on: September 22, 2011, 02:41:31 PM »

Okay, got that bit working, thank you Jason.

Kind of following on from my daft question above, but not so daft as needed now...

If I set a variable within the each() loop, how do I access it outside of the function?
Logged

samhs
Administrator
Hero Member
*****
Posts: 1713



View Profile WWW Awards
« Reply #6 on: September 22, 2011, 02:46:47 PM »

You need to define it outside the function to make it global, then set the value inside the function. With an each() iteration, you don't get to return a value, so that's the only practical way to do it AFAIK
Logged

Loose adj a not held together; not fastened or firmly fixed in place
Lose verb to misplace something. To fail to keep or obtain something, especially because of a mistake, carelessness, etc.
---
Blog: www.ohwrite.co.uk
Twitter: www.twitter.com/samhs
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #7 on: September 22, 2011, 08:28:38 PM »

Cheers Sam. Makes sense smile Will give that a play with tomorrow.
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!