Results 1 to 3 of 3
- 04-17-2010, 06:20 AM #1
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
How can I Pass Variable from JApplet to PHP?
I have tried for the whole night.
And it is not work.
I am familiar with php but not java.
There are no error or any message from Java.
I supposed that the database can connect.
Since PHP has just received the data with simple code, so PHP should be fine.
I guess that the problem should be on java that java did not send the variable.
:confused:
<html>Java Code:public class test extends JApplet { public test(){ JButton myButton = new JButton("sendData"); myButton.setFont(new Font("Sansserif", Font.PLAIN, 14)); myButton.setSize(15, 10); myButton.addActionListener(new button()); add(myButton); } private class button implements ActionListener { public void actionPerformed(ActionEvent e) { PostMsg(10,"hello"); } } public void PostMsg(int score, String name){ try { // Construct data String data = "name" + name + "score" + score; byte[] parameterAsBytes = data.getBytes(); // Send data URL url = new URL("http://localhost/addtest.php"); URLConnection con = url.openConnection(); ((HttpURLConnection) con).setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); //OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream()); OutputStream wr = con.getOutputStream(); wr.write(parameterAsBytes); wr.flush(); } wr.close(); rd.close(); } catch (Exception e) { System.out.println("ERROR"+e.getMessage()); } } }
<title>Untitled Document</title>
</head>
<body>
include("../connectionJAVA/connect.php");
$name = $_POST['name'];
$score =$_POST['score'];
//insert data
$sql = "insert into java
values(null,'$name','$score')";
mysql_query($sql) or die("error=$sql");
</body>
</html>
Java Code:
- 04-18-2010, 12:19 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
There are few errors in this program.
I fix them.
Try with that code.
If you still have a problem, tell us about it.
Java Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.Font; import javax.swing.JApplet; import javax.swing.JButton; import java.net.URL; import java.net.URLConnection; import java.net.HttpURLConnection; import java.io.OutputStream; public class WorkWithPHP { public class test extends JApplet { public test(){ JButton myButton = new JButton("sendData"); myButton.setFont(new Font("Sansserif", Font.PLAIN, 14)); myButton.setSize(15, 10); myButton.addActionListener(new button()); add(myButton); } private class button implements ActionListener { public void actionPerformed(ActionEvent e) { PostMsg(10,"hello"); } } public void PostMsg(int score, String name){ try { // Construct data String data = "name" + name + "score" + score; byte[] parameterAsBytes = data.getBytes(); // Send data URL url = new URL("http://localhost/addtest.php"); URLConnection con = url.openConnection(); ((HttpURLConnection) con).setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); con.setUseCaches(false); //OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream()); OutputStream wr = con.getOutputStream(); wr.write(parameterAsBytes); wr.flush(); // } // wr.close(); // rd.close(); } catch (Exception e) { System.out.println("ERROR"+e.getMessage()); } } } }
- 04-18-2010, 04:48 AM #3
Member
- Join Date
- Apr 2010
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Jframe pass variable to Applet
By wayn in forum AWT / SwingReplies: 0Last Post: 03-10-2010, 09:54 AM -
How to pass a variable to another frame.
By DJCali in forum New To JavaReplies: 10Last Post: 10-14-2009, 03:57 AM -
How pass variable from jsp to servelet
By shiva in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-22-2009, 01:55 PM -
How to pass an html variable as an applet value
By fred in forum Java AppletsReplies: 1Last Post: 08-06-2007, 03:28 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks