Results 1 to 3 of 3
  1. #1
    fantasyme is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default 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:
    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());
    			}
    }
    	}
    <html>


    <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:
      

  2. #2
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    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());
    				} 
    			}
    	}
    }

  3. #3
    fantasyme is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default

    thanks , it works now!
    ;)
    Actually is my php, it is the big problem which I always think it is correct.
    Thanks!

Similar Threads

  1. Jframe pass variable to Applet
    By wayn in forum AWT / Swing
    Replies: 0
    Last Post: 03-10-2010, 10:54 AM
  2. How to pass a variable to another frame.
    By DJCali in forum New To Java
    Replies: 10
    Last Post: 10-14-2009, 04:57 AM
  3. How pass variable from jsp to servelet
    By shiva in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-22-2009, 02:55 PM
  4. How to pass an html variable as an applet value
    By fred in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 04:28 AM
  5. Replies: 1
    Last Post: 08-03-2007, 11:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •