Foros del Web » Programando para Internet » PHP »

LLenar Formulario con input (AJAX)

Estas en el tema de LLenar Formulario con input (AJAX) en el foro de PHP en Foros del Web. Hola amigos a ver si me ayudan con este tema . tengo un formulario , el cual lleno con un input evento (keyup) y me ...
  #1 (permalink)  
Antiguo 27/08/2013, 15:47
 
Fecha de Ingreso: febrero-2013
Mensajes: 21
Antigüedad: 11 años, 2 meses
Puntos: 0
LLenar Formulario con input (AJAX)

Hola amigos a ver si me ayudan con este tema .

tengo un formulario , el cual lleno con un input evento (keyup)
y me carga la consulta por e valor que le ingreso en el input
hasta hay todo bien ,
lo que necesito hacer es que cuando tipee el input ,si no esta el registro en la consulta ,me debe traer/mostrar los inputs igual del formulario para yo llenarlos ,
pero como el ajax trae todo los input cargados de la consulta si el registro no existe
no se me muestran los input y no puedo llenarlos , se entiende?
en el fondo es si no esta el registro al tiepar el input me deben de aparecer igual los demas input para yo llenarlos y hacer el insert
he buscado info pero no encontrado y me he topado con varios ejemplos donde tienen el mismo problema
en la PAGINA2 tengo comentado un codigo al final que es el mismo formulario pero sin el resultado de la consulta , pero no se cmo mostrarlo
para poder llenarlo yo ..

a ver si me ayudan por favor !!
dejo el codigo para ser mas claro:
Saludos...

PAGINA 1:
Código Javascript:
Ver original
  1. <script type="text/javascript">
  2.  
  3.     function showUser(str)
  4.     {  
  5.        
  6.     if (str=="")
  7.  
  8.       {
  9.       document.getElementById("txtHint").innerHTML="";
  10.       return;
  11.       }
  12.     if (window.XMLHttpRequest)
  13.       {// code for IE7+, Firefox, Chrome, Opera, Safari
  14.       xmlhttp=new XMLHttpRequest();
  15.       }
  16.     else
  17.       {// code for IE6, IE5
  18.       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  19.       }
  20.     xmlhttp.onreadystatechange=function()
  21.       {
  22.       if (xmlhttp.readyState==4 && xmlhttp.status==200)
  23.         {
  24.         document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  25.         }
  26.       }
  27.     xmlhttp.open("GET","pagina2.php?q="+str,true);
  28.     xmlhttp.send();
  29.     }
  30. </script>

Código HTML:
Ver original
  1.  
  2. <div id="form">
  3.     <form id="form01" class="formular" method="post" action="usuarios_creados.php" name="form01" >
  4.     <b>Buscar / Crear nuevo Registro</b>
  5.     <br>
  6.     Rut :
  7.     <input value="" class="" type="text" name="numero"  id="numero" maxlength=12 onKeyUp="showUser(this.value)" />
  8.     <br>
  9.  
  10.        <div id="txtHint"></div>
  11.  
  12.         <input class="submit" type="submit" value="Guardar"/><hr/>
  13.     </form>
  14. </div>
  15. </body>
  16. </html>


PAGINA 2:
Código PHP:
<?php include("php_conexion.php"); 


$ms=$_GET["q"];

$sql="SELECT * FROM USUARIO WHERE ID = '".$ms."'";
$objParse oci_parse ($objConnect$sql);
oci_execute ($objParse);
while(
$row oci_fetch_array($objParse))
  {
  
echo 
'  <table border="0">';
echo 
'  <tr>';
echo 
'  <td valign="top">';
echo 
'  Nombre :';
echo 
'  <input value="'.$row['NOMBRES'].'" class="" type="text" name="nombres" id="nombres" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
echo 
'  <br>';
echo 
'  Apellidos :';
echo 
'  <input value="'.$row['APELLIDOS'].'" class="" type="text" name="apellidos" id="apellidos" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
echo 
'    <br>';
echo 
'  telefono :';
echo 
'  <input value="'.$row['TELEFONO'].'" class="" type="text" name="fono" id="fono" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
echo 
'    <br>';
echo 
' telefono fijo :';
echo 
'  <input value="'.$row['TELEFONO_FIJO'].'" class="" type="text" name="fono_2" id="fono_2" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
echo 
'    <br>';
echo 
'  mail:';
echo 
'  <input value="'.$row['MAIL'].'" class="" type="text" name="mail" id="mail" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
   
  }
oci_close($objConnect);


PODRIA MOSTRAR ESTE MISMO FORMULARIO PERO VACIOS  DEBERIA  SER  CON  IF MUESTRE FOMULARIO CON DATOS , ELSE FORMULARIO VACIO Y LLNERARLO  YO ,PERO COMO?


/*
echo '  <table border="0">';
echo '  <tr>';
echo '  <td valign="top">';
echo '  Nombre :';
echo '  <input value="" class="" type="text" name="nombres" id="nombres" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
echo '  <br>';
echo '  Apellidos :';
echo '  <input value="" class="" type="text" name="apellidos" id="apellidos" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
echo '    <br>';
echo '  telefono :';
echo '  <input value="" class="" type="text" name="fono" id="fono" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
echo '    <br>';
echo '  telefono fijo :';
echo '  <input value="" class="" type="text" name="fono_2" id="fono_2" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
echo '    <br>';
echo '  mail:';
echo '  <input value="" class="" type="text" name="mail" id="mail" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
*/



?>

Última edición por Sandiuga; 27/08/2013 a las 16:24
  #2 (permalink)  
Antiguo 27/08/2013, 16:28
 
Fecha de Ingreso: julio-2013
Ubicación: México
Mensajes: 361
Antigüedad: 10 años, 9 meses
Puntos: 55
Respuesta: LLenar Formulario con input (AJAX)

Hola, prueba asi

Código PHP:
Ver original
  1. $row=array();
  2. do{
  3.  
  4. echo '  <table border="0">';
  5. echo '  <tr>';
  6. echo '  <td valign="top">';
  7. echo '  Nombre :';
  8. echo '  <input value="'.$row['NOMBRES'].'" class="" type="text" name="nombres" id="nombres" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  9. echo '  <br>';
  10. echo '  Apellidos :';
  11. echo '  <input value="'.$row['APELLIDOS'].'" class="" type="text" name="apellidos" id="apellidos" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  12. echo '    <br>';
  13. echo '  Empresa :';
  14. echo '  <input value="'.$row['TELEFONO'].'" class="" type="text" name="fono" id="fono" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  15. echo '    <br>';
  16. echo '  Cap. Alta Montana  :';
  17. echo '  <input value="'.$row['TELEFONO_FIJO'].'" class="" type="text" name="fono_2" id="apellidos" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  18. echo '    <br>';
  19. echo '  Cap. Saladillo:';
  20. echo '  <input value="'.$row['MAIL'].'" class="" type="text" name="mail" id="mail" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  21.  }while($row = oci_fetch_array($objParse));

saludos
  #3 (permalink)  
Antiguo 28/08/2013, 08:56
 
Fecha de Ingreso: febrero-2013
Mensajes: 21
Antigüedad: 11 años, 2 meses
Puntos: 0
Respuesta: LLenar Formulario con input (AJAX)

Cita:
Iniciado por Erick_MD9 Ver Mensaje
Hola, prueba asi

Código PHP:
Ver original
  1. $row=array();
  2. do{
  3.  
  4. echo '  <table border="0">';
  5. echo '  <tr>';
  6. echo '  <td valign="top">';
  7. echo '  Nombre :';
  8. echo '  <input value="'.$row['NOMBRES'].'" class="" type="text" name="nombres" id="nombres" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  9. echo '  <br>';
  10. echo '  Apellidos :';
  11. echo '  <input value="'.$row['APELLIDOS'].'" class="" type="text" name="apellidos" id="apellidos" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  12. echo '    <br>';
  13. echo '  Empresa :';
  14. echo '  <input value="'.$row['TELEFONO'].'" class="" type="text" name="fono" id="fono" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  15. echo '    <br>';
  16. echo '  Cap. Alta Montana  :';
  17. echo '  <input value="'.$row['TELEFONO_FIJO'].'" class="" type="text" name="fono_2" id="apellidos" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  18. echo '    <br>';
  19. echo '  Cap. Saladillo:';
  20. echo '  <input value="'.$row['MAIL'].'" class="" type="text" name="mail" id="mail" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
  21.  }while($row = oci_fetch_array($objParse));

saludos
Gracias por responder Erick, probe lo que me dijiste si bien se acerca mas a lo que quiero ,pero no sirve aun , te explico:
cuando digito el id en input si no encuentra el di en la consulta ,claro me muestra los demas inputs vacios y esta bien , pero si ecuentra el id ademas de mostarme los inputs vacios me muestra tambien los demas input con los datos es decir me muestra los dos formularios
uno vacio y otro con datos pero con el mismo ID y da la opcion de llenar el formulario vacio pero el ID ya existe ,no podria hacer el insert
adjunto fotos para ser mas claro ,

lo otro podria ser, dejar siempre los inputs vacios y cuando digite el id me muestre solo los valores de los inputs si tienen datos bien ,sino llenarlos yo pero no se como podria ser .
cualquier ayuda sirve , gracias de nuevo..


FORMULARIO INPUT


CARGA DE LOS DATOS :


Última edición por Sandiuga; 28/08/2013 a las 09:02
  #4 (permalink)  
Antiguo 28/08/2013, 11:42
 
Fecha de Ingreso: julio-2013
Ubicación: México
Mensajes: 361
Antigüedad: 10 años, 9 meses
Puntos: 55
Respuesta: LLenar Formulario con input (AJAX)

Hola, no estoy muy familiarizado con las funciones oci y su flujo, asi que te doy una solucion logica, pero seguramente se puede hacer de mejor forma.

Código PHP:
<?php include("php_conexion.php"); 


$ms=$_GET["q"];

$sql="SELECT * FROM USUARIO WHERE ID = '".$ms."'";
$objParse oci_parse ($objConnect$sql);
oci_execute ($objParse);
$contador=0;//INICALIZAMOS EL CONTADOR
while($row oci_fetch_array($objParse))
{
    
$contador++;//EL CONTADOR INCREMENTA SI ENCUENTRA RESULTADOS SI NO SE QUEDA EN 0
    
resultados($row);
}
oci_close($objConnect);
if(
$contador==0){$row=array();resultados($row);}//SI EL CONTADOR ESTA EN 0 TE IMPRIMIRA LA TABLA VACIA.


function resultados($row){//UNA FUNCION PARA CREAR TU TABLA Y NO REPETIR EL CODIGO
    
echo '  <table border="0">';
    echo 
'  <tr>';
    echo 
'  <td valign="top">';
    echo 
'  Nombre :';
    echo 
'  <input value="'.$row['NOMBRES'].'" class="" type="text" name="nombres" id="nombres" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
    echo 
'  <br>';
    echo 
'  Apellidos :';
    echo 
'  <input value="'.$row['APELLIDOS'].'" class="" type="text" name="apellidos" id="apellidos" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
    echo 
'    <br>';
    echo 
'  telefono :';
    echo 
'  <input value="'.$row['TELEFONO'].'" class="" type="text" name="fono" id="fono" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
    echo 
'    <br>';
    echo 
' telefono fijo :';
    echo 
'  <input value="'.$row['TELEFONO_FIJO'].'" class="" type="text" name="fono_2" id="fono_2" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
    echo 
'    <br>';
    echo 
'  mail:';
    echo 
'  <input value="'.$row['MAIL'].'" class="" type="text" name="mail" id="mail" maxlength="50" onKeyUp="this.value=this.value.toUpperCase();"/>';
}
Saludos
  #5 (permalink)  
Antiguo 28/08/2013, 14:11
 
Fecha de Ingreso: febrero-2013
Mensajes: 21
Antigüedad: 11 años, 2 meses
Puntos: 0
Respuesta: LLenar Formulario con input (AJAX)

Muchas muchas Gracias Erick funciona muy bien , solo tengo un detalle que puede ser un gran detalle ;
veras cuando llamo la pagina esta tiene un submit

Código HTML:
Ver original
  1. echo '  <input class="submit" type="submit" value="Guardar"/><hr/>';

como puedo hacerlo paar que si ,al hacer la consulta no existe registro ademas de mostar los registros vacios aparezca o se habilite el submit del formulario , y si los inputs vienen con registros ocultarlo o deshabilitarlo , porque como esta ahora si hay registros me muestra los datos pero ademas el guardar y al presionarlo generar un error por que ya existe el registro
en eso estoy topando , de nuevo muchas gracias por tu ayuda te pasaste!!
  #6 (permalink)  
Antiguo 28/08/2013, 20:25
Avatar de rocha7778  
Fecha de Ingreso: mayo-2013
Ubicación: Cartagena Colombia
Mensajes: 79
Antigüedad: 11 años
Puntos: 1
Respuesta: LLenar Formulario con input (AJAX)

has escuchado del Modelo Vista Controlador? de procedimientos almacenados?

Ahora que ya resuelvas tus dudas por completos re recomiendo que leas sobre eso,

tus proyectos serán mas seguros y claros, generar código html con php no es una buena practica de programación y realizar consultas con string en la aplicación es inseguro.

Etiquetas: ajax, formulario, html, input, registro, select, sql, usuarios
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:06.