0

I'm trying post data on PHP server through HTML using this HTML code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<form action="http://localhost:/" method="post">
<input type="hidden" value="Expence Description" id="ExpenceDescription" name="ExpenceDescription" />
<input type="hidden" value="DA, TA, Others" id="Descriptions" name="Descriptions" />
<input type="hidden" value="100, 101, 102" id="Amounts" name="Amounts" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

This is my android code:

 HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(URL);

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                            nameValuePairs.add(new BasicNameValuePair("ExpenceDescription", "'" + expenses_head + "'"));
                            nameValuePairs.add(new BasicNameValuePair("Descriptions", "'" + total_desc + "'"));
                            nameValuePairs.add(new BasicNameValuePair("Amounts", "'" + total_amount + "'"));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

 HttpResponse response = httpclient.execute(httppost);
                            HttpEntity entity = response.getEntity();
                            //HttpResponse response = httpclient.execute(httppost);
                            StatusLine statusLine = response.getStatusLine();

The problem is that I'm getting internal server error 500 on response line.

davidrac
  • 10,723
  • 3
  • 39
  • 71
Sudhir
  • 1
  • 1
  • Can we see your Android code as well? In general if you are posting programmatically from a mobile app, you do not use an HTML form - you just grab data using PHP directly, and parse it in PHP. – halfer Oct 05 '15 at 09:14
  • The action URL of `http://localhost:/` is wrong (if you have a colon then I think you must supply a port number) but, as I say, I don't think you need an HTML form at all. – halfer Oct 05 '15 at 09:16
  • I don't have Android code because i don't know how to post data through html. – Sudhir Oct 05 '15 at 09:17
  • OK, step back a bit. What is the problem you are actually trying to solve? Do you want to post data from an Android application to a remote PHP server? Do you have a native Android app in which you want to do this? – halfer Oct 05 '15 at 09:18
  • sorry but I can't share my full url here. – Sudhir Oct 05 '15 at 09:19
  • yes i have a native android app – Sudhir Oct 05 '15 at 09:19
  • 1
    Trying again: **do you want to post data from an Android application to a remote PHP server?** – halfer Oct 05 '15 at 09:20
  • Possible duplicate of [Sending POST data in Android](http://stackoverflow.com/questions/2938502/sending-post-data-in-android) – halfer Oct 05 '15 at 09:24
  • [Useful search](http://stackoverflow.com/search?q=post+android+php+data). – halfer Oct 05 '15 at 09:25
  • You are getting a 500 server error because you do not have any PHP code to respond to the `post` operation. See your Apache server logs for a more detailed error message. – halfer Oct 05 '15 at 09:29

2 Answers2

0

You would need to specify the php file in the action.. for example if your file which listens to the post parameters is called server.php then in form action define as below, assuming both the; this html page and php file reside in the same webserver directory otherwise specify the url for ur php file

<form action="server.php" method="post">

<input type="hidden" value="Expence Description" id="ExpenceDescription" name="ExpenceDescription" />

<input type="hidden" value="DA, TA, Others" id="Descriptions" name="Descriptions" />

<input type="hidden" value="100, 101, 102" id="Amounts" name="Amounts" />

<input type="submit" value="Submit" />

</form>
Dilberted
  • 1,172
  • 10
  • 23
  • HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(URL); – Sudhir Oct 05 '15 at 09:24
  • If you want to use android app to submit post data then in comments of your question, halfer, has posted the answer – Dilberted Oct 05 '15 at 09:30
0

You need a language independent data interchange format like JSON to transfer data from Android to PHP and vice versa.

In PHP you can format your data in json with json_encode and json_decode and in Android you can manage Json with JSONObject and JSONArray

Check out these links (1,2) for well explained examples.

Abey
  • 1,408
  • 11
  • 26