• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to POST variables from Applet to PHP page?

 
Matt Wil
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone show me a few lines of code on how to POST a varible from an applet to a PHP page? My PHP page accepts a varible and writes to a text file. I need to get a variable named "inputString" from the applet to the PHP script. I've tried a few different thing on the web but havent had any luck.
Thanks!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that your question isn't specific to PHP. I believe that you simply want to POST some text to a web server.

If you were satisfied with a GET request, you could get the applet context and then call one of the showDocument methods, passing in the GET request parameter and value as part of the URL.

If you want to POST data from an applet, after opening a new URLConnection from a URL object, use the OutputStream of that URLConnection to write your data in the format "parameter=value". It's probably a good idea to wrap the OutputStream of the URLConnection in a PrintWriter.

If you can't quite figure things out with the above descriptions, just ask more questions. If you've begun work on some code, please don't hesitate to post it to this forum.
 
Matt Wil
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Here is the code.. I think it actually worked one time(didnt work again though)... Also, should I be using the second line(the one I have remarked out) for my String? Thanks!

try{

String inputString = "JustaTest";
//String inputString = URLEncoder.encode("blah blah","UTF-8");

URL postURL = new URL("http://www.example.com/App/LogCustomization.php");
URLConnection connection = postURL.openConnection();
connection.setDoOutput(true);

PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("inputString=" + inputString);
out.close();

}catch(MalformedURLException e){System.out.println("Malformed URL Excpetion Thrown");

}catch(IOException e){System.out.println("IO Exception Thrown");
}
 
Matt Wil
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm.. adding the lines below under the line "out.close();" causes this to work. Aren't the lines below just reading the text and writing to the Java Console? I thought the lines above did the actual writing to the text file?

BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));

String inputLine;

while ((inputLine = in.readLine()) != null) System.out.println(inputLine);

in.close();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic