Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with form autocomplete 1

Status
Not open for further replies.

cybernetic

Programmer
Jul 30, 2001
6
GB
Im trying to write a fnction which automatically completes the address details in a web form, by reloading the page with the values in it. can anyone help me?
 
you don't want to do this, it'll place unnecessary load on your server.

if you are dead set on doing this, though, what you want to do is catch each keypress (use onkeypress and javascript), and throw a query to your database (something like "select name from table where name like 'a%';" where a is the keypress). with this method you have to query the DB at least N times, where N is the number of keypresses. Multiply N by the number of form elements, and the runtime for your document quickly escalates.

Another method you could do is to pull all the possible values from the DB ("select name from table") and store it in javascript. From javascript, you could check the keypresses more easily and more quickly, but the huge drawback to this is that every client gets access to your entire database file, just by opening up the source code.
A bit of a privacy invasion, imho.

These are the only two ways I can think of doing this, short of developing a java applet which stays connected to a db. If you want more information on either of these, post back and I'll be glad to theorize some code on here.

hth
leo

------------
Leo Mendoza
lmendoza@garbersoft.net
 
I'd like to hear anything you have to say about working with a java applet! I don't know much about java, and I'm afraid to use it (it's so powerful!). Do you have some sample code I could see for a basic MySQL query from a java applet?

If I were to "mayscript" the applet, could I not use javascript to send a query through the applet? and javascript to dislay the results?

Basically, if it's not too much trouble, do you think you could show me or point me to the equivalent of:
Code:
<?
Code:
$db_host
Code:
 =
Code:
'localhost'
Code:
;
Code:
$db_user
Code:
 =
Code:
'me'
Code:
;
Code:
$db_pass
Code:
 =
Code:
'password'
Code:
;
Code:
MYSQL_CONNECT
Code:
(
Code:
$db_host
Code:
,
Code:
$db_user
Code:
,
Code:
$db_pass
Code:
);
Code:
MYSQL_SELECT_DB
Code:
(
Code:
'MyDB'
Code:
);
Code:
$results
Code:
 =
Code:
MYSQL_QUERY
Code:
(
Code:
'SELECT id FROM MyTable'
Code:
);
Code:
while
Code:
(
Code:
$row
Code:
 =
Code:
MYSQL_FETCH_ARRAY
Code:
(
Code:
$results
Code:
))
 {
Code:
echo
Code:
Code:
'<HR>'
Code:
.
Code:
$row[0]
Code:
.
Code:
'<BR>'
Code:
;
 }
Code:
MYSQL_CLOSE
Code:
();
Code:
?>

I think that with some carful coding, upgrading one of my programs (to add functionality similar to what cybernetic wants) with a java applet could prove very benificial.

Thanks! -gerrygerry
Go To
 
I know that you can use javascript to connect to an applet, but I've never really done it, so I couldn't be of a lot of help there.

I'm thinking that anything that can run tasks in the background should work (Flash might even work, because I think you can fork a process, but I'm not sure).

Anyhow, here's some sample code that might help. I got this from the mm.mysql site - they supply a jdbc type driver. This is just the Statements.java example code they give, but it should give an idea:(I know this isn't a java forum, so I'll keep it at this - if you're interested in learning, follow the trail at java.sun.com - it's really not that hard, especially when compared to C++ or C ;) )
Code:
/*
 * Statements.java
 *
 * Created on September 11, 2001, 7:41 AM
 */

package testsuite;

import java.sql.*;

/**
 *
 * @author  Administrator
 */
public class Statements {
 
    static String DBUrl = &quot;jdbc:mysql:///test&quot;;
    
    /** Creates new Statements */
    private Statements() 
    {
    }

    public static void main(String[] args) throws Exception
    {
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement pStmt = null;

        try 
        {
            Class.forName(&quot;org.gjt.mm.mysql.Driver&quot;).newInstance();

            conn = DriverManager.getConnection(DBUrl);

            stmt = conn.createStatement();

            try {
                stmt.executeUpdate(&quot;DROP TABLE statement_test&quot;);
            }
            catch (SQLException sqlEx) { /* ignore */}

            stmt.executeUpdate(&quot;CREATE TABLE statement_test (id int not null primary key, strdata1 varchar(255), strdata2 varchar(255))&quot;);
            
            for (int i = 0; i < 10; i++)
            {
            	stmt.executeUpdate(&quot;INSERT INTO statement_test (id, strdata1,strdata2) values (&quot; + i + &quot;, 'abcdefg', 'poi')&quot;);
            }
            
            pStmt = conn.prepareStatement(&quot;UPDATE statement_test SET strdata1=?, strdata2=? where id=?&quot;);
            
            
            pStmt.setString(1, &quot;iop&quot;);
            pStmt.setString(2, &quot;higjklmn&quot;);
            pStmt.setInt(3, 1);
            pStmt.executeUpdate();
	    
	    	PreparedStatement pStmtBad = conn.prepareStatement(&quot;SELECT * from statement_test&quot;);
	    	
	    	pStmtBad.executeQuery().close();
	    	pStmtBad.close();
	    	
	    PreparedStatement pStmtBatch = 
	    	conn.prepareStatement(&quot;INSERT INTO &quot;
	    	+ &quot;statement_test (id, strdata1, strdata2) VALUES (?,?,?)&quot;);
		
	    for (int i = 0; i < 10; i++) {
	    	pStmtBatch.setInt(1, 100 + i);
	    	pStmtBatch.setString(2, &quot;batch_&quot; + i);
	    	pStmtBatch.setString(3, &quot;batch_&quot; + i);
		pStmtBatch.addBatch();
	    }
	    
	    int [] updateCounts = pStmtBatch.executeBatch();
	    
	    System.out.println(&quot;Batch update counts: &quot;);
	    
	    for (int i = 0; i < updateCounts.length; i++) {
		    System.out.println(&quot;  &quot; + updateCounts[i]);
            }
            
            ResultSet rs = stmt.executeQuery(&quot;SELECT * from statement_test&quot;);
            
            while (rs.next())
            {
            	System.out.println(&quot;isAfterLast() = &quot; + rs.isAfterLast());
            }
            
            System.out.println(&quot;isAfterLast() ends at &quot; + rs.isAfterLast());
 
        }
        finally 
        {
            if (pStmt != null)
            {
                try
                {
                    pStmt.close();
                }
                catch (SQLException sqlEx) { /* ignore */ }
            }
            
            if (stmt != null) 
            {
                try 
                {
                    stmt.close();
                }
                catch (SQLException sqlEx) { /* ignore */ }
            }
      
            if (conn != null) 
            {
                try 
                {
                    conn.close();
                }
                catch (SQLException sqlEx) { /* ignore */ }
            }
        }
    }     
}
leo

------------
Leo Mendoza
lmendoza@garbersoft.net
 
thanks gerry -
if you want more information on mm.mysql, the homepage is at mmmysql.sourceforge.net leo

------------
Leo Mendoza
lmendoza@garbersoft.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top