Buenas,
Aquí os pongo un poco el código:
Para empezar la parte del script que ha de estar en la página en la cual esta vuestro formulario (lo dejo con CopyRight que es de una librería que encontré por ahí). Es simple, se encarga de recoger la variable principal que luego se usará en la parta de JSP que va a conectar en la base de datos y filtrar por esa variable y mostrar la información restante en el forms, además se encarga de lanzar la función automáticamente:
Código PHP:
/************************************************************************************************************
Ajax client lookup
Copyright (C) 2006 DTHMLGoodies.com, Alf Magne Kalleland
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
written by Alf Magne Kalleland.
Alf Magne Kalleland, 2006
Owner of DHTMLgoodies.com
************************************************************************************************************/
var ajax = new sack();
var currentClientID=false;
function getClientData()
{
var clientId = document.getElementById('nombre').value;
currentClientID = clientId;
ajax.requestFile = 'getClient.jsp?getClientId='+clientId; // Specifying which file to get
ajax.onCompletion = showClientData; // Specify function that will be executed after file has been found
ajax.runAJAX(); // Execute AJAX function
}
function showClientData()
{
var formObj = document.forms['solicitar_reparacion'];
eval(ajax.response);
}
function initFormEvents()
{
document.getElementById('nombre').onblur = getClientData;
document.getElementById('nombre').focus();
}
window.onload = initFormEvents;
</script>
El formulario donde esta la variable principal "nombre" y cual se rellena posteriormente:
Código PHP:
<h1>Solicitar reparación</h1>
<form name="solicitar_reparacion">
<table id="reparacion">
<tr><th colspan="5">Datos cliente</th></tr>
<tr><td>Nombre</td><td colspan="4"><input name="nombre" id="nombre" type="text" /></td></tr>
<tr><td>Apellido</td><td colspan="4"><input name="apellido" type="text" /></td></tr>
<tr><td>Teléfono</td><td colspan="4"><input name="telefono" type="text" /></td></tr>
<tr><td>Correo</td><td colspan="4"><input name="correo" type="text" /></td></tr>
</table>
Y la parte de jsp que se conecta a la BBDD para coger la información deseada (utilizo un properties para las variables de la BBDD):
Código PHP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page session="true" %>
<%@page import="java.util.ResourceBundle" %>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.util.ResourceBundle" %>
<% ResourceBundle rb = ResourceBundle.getBundle("WEB");
ResultSet sr = null; %>
<% try {
java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conexion = DriverManager.getConnection(rb.getString("URLDB"), rb.getString("USERDB"),rb.getString("PASSDB"));
java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Statement st = conexion.createStatement();
sr = st.executeQuery("select * from clientes where cli_nombre = '"+request.getParameter("getClientId").toString()+"'");
while (sr.next()) {
out.print("formObj.apellido.value = '"+sr.getObject("CLI_APELLI")+"';\n");
out.print("formObj.correo.value = '"+sr.getObject("CLI_EMAIL")+"';\n");
out.print("formObj.telefono.value = '"+sr.getObject("CLI_TELEFO")+"';\n");
}
conexion.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
Cualquier duda me comentais.
Saludos!