Pass string array to PHP script as POST

Andrea Bellitto

I am trying to pass a string array to a PHP script as POST data in an Android app but i have an issue.

this is my client side code:

ArrayList<NameValuePair> querySend = new ArrayList<NameValuePair>();
for (Stop s : stops) {
                querySend.add(new BasicNameValuePair("stop_data[]", s.getNumber()));
            }
HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.myURL/myPHPScript.php");
                httppost.setEntity(new UrlEncodedFormEntity(querySend));
                HttpResponse response = httpclient.execute(httppost);

and this is my PHP script code:

foreach($_GET['stop_data'] as $stop) {
  // do something
}

If i run my java code i will get this response error from php script:

Warning: Invalid argument supplied for foreach() in /web/htdocs/www.myURL/myPHPScript.php on line 6
null

otherwise if i will put the request on my browser manully in this way:

http://www.myURL/myPHPScript.php?stop_data[]=768&stop_data[]=1283

i will give the correct answer!

if i try the same code, but if i POST not an array but a single string passing and receiving stop_data variables without [] it works! I can't understand where is the mistake!! It is possible that the [] in BasicNameValuePair String name are doing some codification mistaken? I need to pass an array, how i have to do?

Patato

you use POST method and in php code you should use $_POST

try this

foreach($_POST['stop_data'] as $stop) {
  // do something
}

this way is using GET method

http://www.myURL/myPHPScript.php?stop_data[]=768&stop_data[]=1283

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to pass associative array to a php page in POST?

From Dev

How to pass a PHP array to a BASH script?

From Dev

Pass an array to a php script using AJAX

From Dev

Pass an array from a php script to another

From Dev

How to pass Object or array with Query string in PHP

From Dev

Pass php string as parameter for javascript function called in php script

From Dev

How to pass values to string array in post method using Fiddler?

From Dev

how to pass php post method string in javascript parameter function?

From Dev

How to pass data of many textbox values with POST array in PHP

From Dev

run script from command line and pass String array

From Dev

Pass html input array values to php script via ajax

From Dev

Sending array from android to php. $_POST shows Array() string

From Dev

Pass a php variable in a script

From Dev

PHP Post script not working

From Dev

AJAX POST to PHP script

From Dev

Parse a query string to be sent as an array in a silent post (php)

From Dev

PHP Convert Multi-dimensional Array to Query String for curl POST

From Dev

Why my PHP Curl POST request turns data array into string?

From Dev

Pass Array of Objects in http Post

From Dev

Pass array of objects to POST in Angular

From Dev

Pass POST data into a PHP function

From Dev

Pass POST data into a PHP function

From Dev

PHP Array pass to function

From Dev

Pass php array into HTML

From Dev

Pass script to cscript/wscript as string

From Dev

pass php array to javascript array

From Dev

Pass variable to php script with AJAX

From Dev

Pass JavaScript variable to php script

From Dev

Resolve PHP post string