Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_php thread: RE: calling a php function from javascript function


Message #1 by "Richard D Shank" <richard@c...> on Wed, 31 Jul 2002 13:59:56
I once wrote a small java applet that does this job. Javascript calls 
an applet method with a parameter string. The applet opens an http: 
connection to my php code which does anything that php can do and 
returns a status string that is interpreted by the javascript (I 
defined a text based data structure). Since java applets naturally 
have a display window, I can also use it to display status results.

My most productive use of this technology is in an order processing 
system. It gets status info about a customer as you tab out of the 
customer number field. Very nice.

It is also used to retrieve a bunch of appropriate options depending 
on a product selection. A popup menu presents the initial options. 
Once that is selected, the applet looks up a list of subordinate 
alternatives, sends them to javascript which revises other popup 
menus on the page.

A third use, just for fun, is on my website, justkidding.com, where I 
use it for 'Instant Email'. With this, you enter an email address and 
message, then click send. The applet transmits the info to a php page 
with a mail() function. It's kind of cool because none of these 
examples need to refresh the page to get the job done.

It took me about a week to come up with the first implementation from 
scratch. I am enclosing a complete example.  Hopefully, this will 
make it quicker for you. It's a very cool technology and really 
extends the integration between php and the rest of the world.

Notes:

1) As a Macintosh person, I used a free java compiler 'javac'. I 
assume there are good alternatives for windows users.

2) You can't call an applet in another domain. Java security won't 
let you (though I suppose it's possible if you use a signed applet or 
something like that).

3) I revised the URLs in this example so that they don't point to my 
real stuff. If you want to see it work, send me a message from 
http://justkidding.com by going to the bottom of the page and trying 
it out. I'll send a message back if you put your email address in the 
signature field.


Good luck,
tqii


THE APPLET ==========================================

note "also, the procedure to make this go, render the applet to disk, 
use 'javac' to compile the applet and upload into appletDirectory (as 
spec'd in the end user page)"

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.io.*;

public class mailApplet extends java.applet.Applet
    -(-
       private String inputLine=new String("");
       private String outputString=new String("");
       private String URLString=new String("");
       public void init()
          -(-
             try
                -(-
                   outputString="Enter Message Below";
                   -)-
                catch (Exception e)-(- -)-;
             -)-
       public String sendd(String emailAdr, String body, String 
signature, String websiteSignature) throws Exception
          -(- //, String body, String signature, String websiteSignature
             outputString="";
             repaint();
             String args=new 
String("operation="+URLEncoder.encode("transmitMail"));
                args+="&emailAdr="+URLEncoder.encode(emailAdr);
                args+="&body="+URLEncoder.encode(body);
                args+="&signature="+URLEncoder.encode(signature);
                args+="&websiteSignature="+URLEncoder.encode(websiteSignature);
             String sendProcessURL = {"\\\""+getURL("sendMail", true)+"\\\""};
                sendProcessURL+="?"+args;
             URL urlObject= new URL(sendProcessURL);
             URLConnection connection=null;
             InputStream inputData=null;
             try
                -(-
                   connection=urlObject.openConnection();
                   connection.setDoOutput(true);
                   inputData=connection.getInputStream();
                   -)-
                catch (Exception e)-(- 
outputString=outputString+"error zone "+e; -)-;
             //outputString=outputString+"1";
            
             String contentType=connection.getContentType();
             if (contentType.toLowerCase().startsWith("text"))
                -(-
                   BufferedReader in=new BufferedReader(new 
InputStreamReader(inputData));
                  
                   try
                      -(-
                         while ((inputLine = in.readLine()) != null) 
-(- outputString=outputString+inputLine+"\n"; -)-
                         in.close();
                         -)-
                      catch (Exception e)-(- 
outputString=outputString+"error zone 2"+e; -)-;
                -)-
             repaint();
             return outputString;
             -)-
       public String test(String s) throws Exception
          -(-
             outputString="";
             String processPage = "http://www.justkidding.com/processPage.php";
             if (s.equals("")) -(- s="bunko"; -)-
             String args=new String("msg="+URLEncoder.encode(s));
             processPage+="?"+args;
             URL urlObject= new URL(processPage);
             URLConnection conn=null;
             OutputStream theControl=null;
             InputStream inputData=null;
             try
                -(-
                   conn=urlObject.openConnection();
                   conn.setDoOutput(true);
                   inputData=conn.getInputStream();
                   //outputString=inputData+"ok world 1";
                   -)-
                catch (Exception e)-(- 
outputString=outputString+"error zone "+e; -)-;
             //outputString=outputString+"1";
            
             String contentType=conn.getContentType();
             if (contentType.toLowerCase().startsWith("text"))
                -(-
                   BufferedReader in=new BufferedReader(new 
InputStreamReader(inputData));
                  
                   try
                      -(-
                         while ((inputLine = in.readLine()) != null) 
-(- outputString=outputString+inputLine+"\n"; -)-
                         in.close();
                         -)-
                      catch (Exception e)-(- 
outputString=outputString+"error zone 2"+e; -)-;
                -)-
             repaint();
             return s;
             -)-
       public void paint(Graphics g)
          -(-
             g.setColor(Color.blue);
             g.drawString(outputString, 10, 10);
             -)-



THE HTML: APPLET INVOCATION =============================================


\<APPLET
code=mailApplet.class
codeBase='http://www.justkidding.com/projects/appletDirectory'
width=550
height=15
name='mailApplet'
>
</APPLET>



THE HTML: INPUT FORM =============================================


<TR>
   <TD>
     <INPUT type=text name='emailAdr' value='feedback@j...' size=20>
     </TD>
   <TD>
     <INPUT type=text name='body' value='Type Your Comments Here' size=40>
     </TD>
   <TD>
     <INPUT type=text name='signature' value='Love & Kisses, Signature' size=25>
     </TD>
   <TD>
     <INPUT TYPE=button value='Send' 
onclick='submitForm1a(document.form1Name.emailAdr.value, 
document.form1Name.body.value, document.form1Name.signature.value, 
document.form1Name.emailAdr)')>
     </TD>
   </TR>



THE JAVASCRIPT =========================================

function submitForm1a(emailAdr, body, signature, resultObject)
    -(-
       var outputString;
       var websiteSignature="\n\r\n\r===========================";
       websiteSignature+="\n\r\n\rThis message was sent with Instant 
Email from "+document.URL;
       websiteSignature+="\n\r\n\rIf you have any problems send mail 
to problems@j..."
       outputString=document.mailApplet.sendd(emailAdr, body, 
signature, websiteSignature);
       //resultObject.value=outputString; //if we want to return the 
result to a formfield
       resultObject.value=""; //if we want to return the result to a formfield
       return true;
    -)-

-- 

--------------------------------------------------------------------------------

We protect data, the 21st century asset.

TQ White II
Chief Operating Officer
Backup Bank, LLC

312/566-0600

tq@B...
http://beSafe.BackupBank.com

--------------------------------------------------------------------------------

  Return to Index