0

I've studied up on the Oracle documentation and examples and still can't get this to work.

I have a Java Applet that is simply trying to send a text field to a PHP script via POST, using URLConnection and OutputStreamWriter. The Java side seems to work fine, no exceptions are thrown, but PHP is not showing any output on my page. I am a PHP noob so please bear with me on that part.

Here is the relevant Java portion:

    try {
            URL url = new URL("myphpfile.php");
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            out = new OutputStreamWriter(con.getOutputStream());
            String outstring = "field1=" + field1 + "&field2=" + field2;
            out.write(outstring);

            out.close(); 

    }
    catch (Exception e) {
        System.out.println("HTTPConnection error: " + e);
        return;
    }

and here is the relevant PHP code:

    <?php
            $field1= $_POST['field1'];
            $field2= $_POST['field2'];
            print "<table><tr><th>Column1</th><th>Column2</th></tr><tr><td>" .
                 $field1 . "</td><td>" . $field2 . "</td></tr></table>";
    ?>

All I see are the table headers Column1 and Column2 (let's just keep these names generic for testing purposes). What am I doing wrong? Do I need to tell my PHP script to check when my Java code does the write?

3
  • 1
    you dont send POST request, look at stackoverflow.com/questions/4205980/… Commented Nov 5, 2013 at 9:45
  • Thanks. After reading that, it seems like the only difference is that I need to explicity set "con.setRequestMethod("POST");" correct? Commented Nov 5, 2013 at 16:33
  • Unfortunately, none of the techniques on that question had any effect for me. Commented Nov 6, 2013 at 4:40

2 Answers 2

1

Not USE $_POST ,USE $_REQUEST OR $_GET

WHERE TO SET $field1 and $field2 in your php script?

Try URL url = new URL("myphpfile.php?field1=" + field1 + "&field2=" + field2);

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

3 Comments

Thank you, I will try that out. Sorry, my variable names were incorrect. I have edited it!
The GET/REQUEST did not work either. I wonder if it's a problem with how I write the php output to the page that the applet is on? It doesn't seem to update the page with the text at all. The php code is referenced in my html page via an iframe...is there a better way to do that?
Yep, I already tried passing the fields in via the URL and PHP still didn't pick it up. I decided to use Java=>Javascript=>PHP instead which works.
0

Well, I feel like I've tried every possible thing that can be tried with PHP, so I eventually went with JSObject. Now THAT was easy.

Working Java code:

JSObject window = JSObject.getWindow(this);

// invoke JavaScript function
String result = "<table><tr><th>Column1</th><th>Column2</th></tr><tr><td>" 
+ field1 + "</td><td>" + field2 + "</td></tr></table>";
window.call("writeResult", new Object[] {result});

Relevant working Javascript:

function writeResult(result) {
        var resultElem =
            document.getElementById("anHTMLtagID");
        resultElem.innerHTML = result;
    }

From here I can even send the results from Javascript to PHP via Ajax to do database-related actions. Yay!

Comments

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.