Java Code

formEmail.php


<?php $request_method = $_SERVER["REQUEST_METHOD"]; if ( $request_method == "GET" ) { $query_vars = $_GET; } elseif ( $request_method == "POST" ) { $query_vars = $_POST; } reset ( $query_vars ); $emailbody = "New Form Submission: "; while ( list ($key, $val ) = each ( $query_vars ) ) { $emailbody = "$emailbody \n $key = $val" ; } $emailto = 'info@coffeeClubOfTheWorld.com'; $emailfrom = 'info@coffeeClubOfTheWorld.com'; $subject = "New Form Submission"; mail ( $emailto, $subject, $emailbody, "From: $emailfrom" ); ?>

SendFormAsEmailPHP.java


import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class SendFormAsEmailPHP extends JApplet implements ActionListener { JPanel toppane; JScrollPane scrollPane; JButton submit; JTextField tf_name; JCheckBox cb_funny; JCheckBox cb_happy; JRadioButton rb_red, rb_blue, rb_green; ButtonGroup colorGroup; JLabel l_name, l_funny, l_happy, l_colors; JPanel formPanel; public void init( ) { setLayout( new BorderLayout( ) ); setupButton( ); setupForm( ); } public void setupForm( ) { l_name = new JLabel( "Name:", JLabel.RIGHT ); l_colors = new JLabel( "Favorite Color:", JLabel.RIGHT ); tf_name = new JTextField( 10 ); cb_funny = new JCheckBox( "Funny?" ); cb_happy = new JCheckBox( "Happy?" ); rb_red = new JRadioButton( "red" ); rb_blue = new JRadioButton( "blue" ); rb_green = new JRadioButton( "green" ); colorGroup = new ButtonGroup( ); colorGroup.add( rb_red ); colorGroup.add( rb_blue ); colorGroup.add( rb_green ); formPanel = new JPanel( new GridLayout(3,2) ); formPanel.add( l_name ); formPanel.add( tf_name ); JPanel p_cb = new JPanel( new FlowLayout( ) ); p_cb.add( cb_funny ); p_cb.add( cb_happy ); formPanel.add( p_cb ); formPanel.add( cb_happy ); formPanel.add( l_colors ); JPanel p_colors = new JPanel( new FlowLayout( ) ); p_colors.add( rb_red ); p_colors.add( rb_blue ); p_colors.add( rb_green ); formPanel.add( p_colors ); add( formPanel, BorderLayout.NORTH ); } public void setupButton( ) { submit = new JButton( "Submit" ); submit.addActionListener( this ); toppane =new JPanel( new FlowLayout( ) ); toppane.add( submit ); add( toppane, BorderLayout.SOUTH ); } public void actionPerformed( ActionEvent ae ) { Object src = ae.getSource( ); if( src == submit ) { invalidate( ); String email = "Form Submission\n"; email += "\n name = " + tf_name.getText( ); email += "\n funny? = " + cb_funny.isSelected( ); email += "\n happy? = " + cb_happy.isSelected( ); email += "\n color = " + rb_red.isSelected( ); email += "\n color = " + rb_red.isSelected( ); email += "\n color = " + rb_red.isSelected( ); sendMail( "http://www.cs.colostate.edu/~boese/JavaApplets/Code/ch18_Internet/formEmail.php?message=", email ); formPanel.removeAll( ); JLabel thanks = new JLabel( "Thanks for your input!" ); formPanel.add( thanks ); validate( ); } } public String sendMail( String fullPath, String msg ) { try { String enc = URLEncoder.encode(msg, "UTF-8"); URL target = new URL( fullPath + enc ); String content; URLConnection con = target.openConnection( ); // open connection con.setUseCaches( false ); con.setDefaultUseCaches( false ); byte b[ ] = new byte[ 1024 ]; // byte array int nbytes; // num bytes read in String retVal = new String(); BufferedInputStream in = new BufferedInputStream( con.getInputStream(), 2048 ); while( (nbytes = in.read( b, 0, 1024 )) != -1 ) // while there's more data to read { content = new String( b, 0, nbytes ); // get 1024 bytes of data fr file retVal += content; } in.close( ); // close connection return retVal; } catch( Exception e ) { return "Error " + e.toString( ); } } }

©2006 by E.S.Boese. All Rights Reserved