3

Problem:

I have an array in JS that looks like this:

["intelligence", "skin", "weight", "volume"]

This is stored in a var called "newListASource". I apply this var to a form and then submit it using POST.

// Form destination
var formUrl     = 'explorer.php',

// Generate the form
$form = $("<form action='"+ formUrl +"' method='post'></form>")
        .append("<input type='hidden' name='Perspective' value='" + newListASource + "'>")
        .append("<input type='hidden' name='form_submitted' value='true'>")
        .appendTo('body');

// Submit the form
$form.submit();

When I pick it up through PHP and print it out, it gives me a string with commas. For instance:

skin,weight,intelligence,volume

Desired output I would like is (with trimmed spaces in the beginning and end):

skin weight intelligence volume
2
  • A side question: What did you mean with "pick it up through php"? Commented Nov 20, 2013 at 9:34
  • @reporter The form is submitted using POST to a PHP page. On that PHP page I echo the POST variable Perspective and I get the values in the form of: skin,weight,intelligence,volume Commented Nov 20, 2013 at 9:37

7 Answers 7

5

Since you are using an array use Array.join() with the separator as ' '

"<input type='hidden' name='Perspective' value='" + newListASource.join(' ') + "'>"
Sign up to request clarification or add additional context in comments.

Comments

5

newListASource.join(' ') will give you the desired result -

// Generate the form
$form = $("<form action='"+ formUrl +"' method='post'></form>")
    .append("<input type='hidden' name='Perspective' value='" 
        + newListASource.join(' ') + "'>")
    .append("<input type='hidden' name='form_submitted' value='true'>")
    .appendTo('body');

// Submit the form
$form.submit();

See the doc.

Currently you are getting comma separated values because newListASource.toString() is called whenever you are concatenating your array with a string, and this is the default behavior of toString method for arrays. When you call join, however, it converts your array to a string, separated by the delimiter that you pass to it as argument. So you get values separated by spaces in this case.

Comments

3

Use .join()

newListASource.join(' ')

Comments

2

You can use .join() and $.trim():

// Form destination
var formUrl     = 'explorer.php',
    newListASource = newListASource.join(' ');

then in the form you can change to this:

$.trim(newListASource)  

Comments

0
 var arr = ["intelligence", "skin", "weight", "volume"];


 var result = ary.join(',');

Comments

0

Simply like that:

arr.join("")

Comments

0

What you can also do is just send the array as an array!

var formUrl     = 'explorer.php';
var $form = $("<form action='"+ formUrl +"' method='post'></form>")
    .append("<input type='hidden' name='form_submitted' value='true'>")
    .appendTo('body');
while(newListASource.length)
    $form.append($("<input type='hidden' name='Perspective[]' value=''>").val(newListASource.shift()));
$form.submit();

Php will then actually have an array inside $_POST['Perspective']. (Beware the input name is now Perspective[ ] to indicate it will be an array.)

Comments

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.