0

I have a variable (pvlist) an array which contains data like so '120123,345654'. When I try passing only one value using window.open it works e.g when the array only has one value. If I have more than one value in my array, I get a blank page with nothing. I have tried various solutions suggested which include:

window.open("../Functions/csv.php?refno="+pvlist);
window.open("../Functions/csv.php?refno="+pvlist+"");
window.open("../Functions/csv.php?refno="+pvlist.join(",")+""); 

Any suggestion of what can be used to pass a variable with more than one comma separated value? all help appreciated.

EDIT variable pvlist is a variable with comma separated values

9
  • 2
    Does pvlist contain ['120123,345656'] or [120123,345656]? Commented Aug 15, 2012 at 11:42
  • 4
    It should be csv.php problem, not javascript one. And +"" part is essentially a no-op. Commented Aug 15, 2012 at 11:43
  • Is the idea for refno to contain a single string with all of the values? Or do you want to handle it as an array in your PHP? Commented Aug 15, 2012 at 11:53
  • @SuneTrudslev pvlist contains data as 120123,345656. The items are being pushed into a variable of type []. var pvlist = []; Commented Aug 15, 2012 at 12:04
  • 1
    And does your PHP correctly handle refno as a string with commas in it? Commented Aug 15, 2012 at 12:22

2 Answers 2

1

You need the join method of array. That turns an array like this [1,2,3,4,5] into a string like this "1,2,3,4,5" (or whatever other separator you need)

Example:

window.open("../Functions/csv.php?refno=" + pvlist.join(','));
Sign up to request clarification or add additional context in comments.

8 Comments

If pvlist is an array, then it does not have .split method. Only strings can be split.
Further to what Felix said, the method you are looking for is .join().
Erm... may be I'm missing something, but why do you ignore the fact that Array.join(',') is essentially the same as Array.toString()? ) The OP's problem is not in JS, otherwise its first approach would work just fine.
I was not aware array.toString() is equivalent to array.join. Non the less, you should use .join() for readability if nothing else.
"somestring" + pvlist concatenates the result of pvlist.toString() to the end of "somestring" where the pvlist elements will be joined with commas. So explicitly joining the array with .join(',') produces the same result though I agree it is more readable. But in any case OP already tried with .join(",") as shown in the question.
|
0

You can try using escape function for passing values. Then you can always decode later. Works fine. I had to pass comma separated values from JS file back to controller.

var fee = //comma separated value; var data = escape(fee);

In java you can always decode this value using URLdecoder.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.