0

I've been struggling on how to solve this. I wasn't able to find any solutions over the internet.

This a part of the code:

var params = "nome=" + encodeURI(document.getElementById("nome").value )+
"&email=" + encodeURI(document.getElementById("email").value )+
"&telefone=" + encodeURI(document.getElementById("telefone").value )+
"&produto=" + encodeURI(document.getElementsByName("produto[]") )+
"&quantidade=" + encodeURI(document.getElementsByName("quantidade[]") )+
"&msg=" + encodeURI(document.getElementById("msg").value );
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close"); 

"produto" and "quantidade" are arrays, coming from a form. How to pass this values to my php page (I want to send the content via email).

3
  • HTML Forms doesn't have arrays. Could you elaborate on what you're trying to accomplish, perhaps show us your HTML? Commented Jul 26, 2011 at 19:45
  • if they are check boxes it could be passed to php as an array I believe Commented Jul 26, 2011 at 19:48
  • what are the form elements that have the names produto[] and quantidade[] Commented Jul 26, 2011 at 19:53

3 Answers 3

2

using jquery the serialize() function will turn your array into a string to pass to your server

$('[name=produto]').serialize()

http://api.jquery.com/serialize/

Sign up to request clarification or add additional context in comments.

2 Comments

Far easier to let jquery handle the form serialization/ajax stuff than trying to roll your own. +1.
That's great, using serialize makes it way simple! Awesome, my application is working like a charm now. Thank you guys for the replies.
0

I briefly remember if you do this: (this is also how jQuery serialize works)

var myArrayToPost = [1, 2, 3];
var postString = "";
for(<-- Iterate over myArrayToPost -->) {
   postString += "MyArray[]=" + value + "&";
}
<-- Post postString -->

Basically you want the post to be this:

"MyArray[]=FirstValue&MyArray[]=SecondValue&MyArray[]=ThirdValue"

Then PHP will automatically put that into an array in $_POST so you can get it:

$_POST['MyArray'] // which will equal 
                  // array(
                  //   'FirstValue',
                  //   'SecondValue', 
                  //   'ThirdValue'
                  // );

Comments

0

When you pass parameters in http request, each one of them is a pair name=value, so produto=1 is a parameter produto with 1 as its value. To take an "array" from http request you must build the several parameters with same name, something like produto=1&produto=2& ...

2 Comments

Ademir, acabei optando pelo "serialize", muito simples e funcional, obrigado.
Não conhecia esta solução, creio que vou adotar também, abraços Paulo.

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.