1

i am using web service to send user information to MySQL but PHP doesn't return posted values when i am trying to echo the values, and gives me:

Notice: Undefined index: user_name in C:\xampp\htdocs\webservice\sign_up.php on line 5

my code :

    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {

            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost("http://10.0.2.2/webservice/sign_up.php");
            ArrayList<NameValuePair> post = new ArrayList<NameValuePair>(3);
            post.add(new BasicNameValuePair("user_name", name));
            post.add(new BasicNameValuePair("user_email", email));
            post.add(new BasicNameValuePair("user_password", password));
            request.setEntity(new UrlEncodedFormEntity(post));
            HttpResponse response = client.execute(request);

            int status = response.getStatusLine().getStatusCode();  
            Log.i("data posted, status = " , Integer.toString(status)); 

        } catch (ClientProtocolException e) {  
            // TODO Auto-generated catch block  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
        }  

        return null;


    }
}

and PHP :

<?php
$user = $_POST["user_name"];
echo $user;
?>
12
  • print_r($_POST); what does it show? Commented Mar 4, 2014 at 13:24
  • recurrent problem it seems. We had the same question a few days ago. Commented Mar 4, 2014 at 13:25
  • It clearly says: Notice: Undefined index: user_name in C:\xampp\htdocs\webservice\sign_up.php on line 5 Commented Mar 4, 2014 at 13:26
  • show var_dump($_POST); if possible Commented Mar 4, 2014 at 13:26
  • i used print_r($_POST); it gives me : Array() Commented Mar 4, 2014 at 13:45

2 Answers 2

1

Sample code to pass string from android client to PHP

Android Client part

The code below will enable your android app to talk to a webpage.

Note : You also need to enable uses permission for internet in the android.manifest file.

<uses-permission
        android:name="android.permission.INTERNET" />

Code for android app

public void postData(String toPost) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.URL.com/yourpage.php");

//This is the data to send
String MyName = 'adil'; //any data to send

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("action", MyName));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);

//This is the response from a php application
String reverseString = response;
Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();

} catch (ClientProtocolException e) {
Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
} catch (IOException e) {
Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
}

}//end postData()

PHP code

<?php

echo $_POST["action"];

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

Comments

0

Can you do one of the following:

  • print_r($_POST);
  • Use a program like fiddler to capture traffic, and see if you POST request is being sent

3 Comments

print_r($_POST); gives me : Array()
and i used fiddler, the request doesn't return anything
anything else? @Aaron

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.