JQuery - Duda al rellenar textbox a partir de combobox usando php/ajax/jquery

 
Vista:
Imágen de perfil de beto

Duda al rellenar textbox a partir de combobox usando php/ajax/jquery

Publicado por beto (1 intervención) el 10/04/2014 02:38:52
Tengo una aplicacion en php que estoy realizando, el problema que tengo es el siguiente...

Tengo una pagina llamada modificar.php en la cual aparece un Combobox con los usuarios listados en la base de datos MySql

Al seleccionar algun usuario, quiero que me abra el formulario para poder modificar datos de estos ususarios, los datos apareceran en varios textbox.

Se que se debe utilizar Ajax, pero la verdad estoy teniendo muchas dudas al momento de intentarlo, les dejo el codigo que estoy utilizando.

Modificar.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script>
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
	  <?php
session_start();
include_once "conexion.php";
?>
<style>
		*{
			font-size: 14px;
			font-family: sans-serif;
		}
		form.registro{
		    background: none repeat scroll 0 0 #F1F1F1;
		    border: 1px solid #DDDDDD;
		    margin: 0 auto;
		    padding: 20px;
		    width: 278px;
		}
		form.registro div {
		    margin-bottom: 15px;
		    overflow: hidden;
		}
		form.registro div label {
		    display: block;
		    float: left;
		    line-height: 25px;
		}
		form.registro div input[type="text"], form.registro div input[type="password"] {
		    border: 1px solid #DCDCDC;
		    float: right;
		    padding: 4px;
		}
		form.registro div input[type="submit"] {
		    background: none repeat scroll 0 0 #DEDEDE;
		    border: 1px solid #C6C6C6;
		    float: right;
		    font-weight: bold;
		    padding: 4px 20px;
		}
		.error{
			color: red;
		    font-weight: bold;
		    margin: 10px;
		    text-align: center;
		}
</style>
  </head>
  <body>
  <center>
  <form> <div><label>Seleccione el Usuario:</label>
    <select name="username" id="username" onchange="showUser(this.value)">
	<?php
	$sql = 'SELECT * FROM users ORDER BY id ASC';
    $result=mysql_query($sql);
	while($Rs=mysql_fetch_array($result)) {
	echo "<option value='".$Rs['username']."'>".$Rs['username']."</option>";
	}
	?>
    </select>
  </div></form></center>
  <br>
<div id="txtHint"><b></b></div>
 
</body>
</html>

Se supone segun el codigo ajax que estoy usando, la variable se debera guardar en "q" en el archivo getuser.php

El cual queda de la siguiente forma

Getuser.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
session_start();
include_once "conexion.php";
?>
<style>
        *{
            font-size: 14px;
            font-family: sans-serif;
        }
        form.registro{
            background: none repeat scroll 0 0 #F1F1F1; 
            border: 1px solid #DDDDDD; 
            margin: 0 auto;
            padding: 20px;
            width: 278px;
        }
        form.registro div {
            margin-bottom: 15px;
            overflow: hidden;
        }
        form.registro div label {
            display: block;
            float: left;
            line-height: 25px;
        }
        form.registro div input[type="text"], form.registro div input[type="password"] {
            border: 1px solid #DCDCDC; 
            float: right;
            padding: 4px;
        }
        form.registro div input[type="submit"] {
            background: none repeat scroll 0 0 #DEDEDE; 
            border: 1px solid #C6C6C6; 
            float: right;
            font-weight: bold;
            padding: 4px 20px;
        }
        .error{
            color: red;
            font-weight: bold;
            margin: 10px;
            text-align: center;
        }
</style>
<form action="" method="post" class="registro">
 
<div><label>Usuario:</label><input type='"text"' name='"user"' value="<?php
    $q = intval($_GET['q']); 
    $sql="SELECT * FROM users WHERE id = '".$q."'";
    $result=mysql_query($sql);
    while($Rs=mysql_fetch_array($result)) {
    echo $Rs["username"]; }
?>"/></div>

  
<div><label>Clave:</label> 
    <input name="password" type="text">
  </div> 
<div><label>Email:</label> 
    <input name="email" type="text">
  </div> 
<div><label>Nombre:</label> 
<input type="text" name="nombre"></div>
<div><label>Nivel</label> 
<select name="nivel">
  <option value="Admin">Administrador</option>
  <option value="Super">Supervisor</option>
  <option value="Mostrador">Mostrador</option>
</select></div> 
<div><label>Ubicacion:</label> 
<select name="ubicacion">
      <option value="General">General</option>
      <option value="Torreon">Torreon</option>
      <option value="Saltillo">Saltillo</option>
      <option value="Monterrey">Monterrey</option>
      <option value="Aguascalientes">Aguascalientes</option>
      <option value="Chihuahua">Chihuahua</option>
    </select></div> 
<div> 
<input type="submit" name="enviar" value="Modificar"></div>
</form>

Gracias de antemano
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 302
Oro
Ha mantenido su posición en JQuery (en relación al último mes)
Gráfica de JQuery

Duda al rellenar textbox a partir de combobox usando php/ajax/jquery

Publicado por xve (673 intervenciones) el 10/04/2014 09:14:07
Hola Beto, en las paginas cargadas con ajax, no es necesario volver a definir los estilos, ya que cogerá los estilos de la pagina donde se muestren.

Te recomiendo utilizar jquery, con una simple linea tienes realizado toda la gestión del ajax... seria tan sencillo como:
1
2
3
4
5
6
<script>
function showUser(str)
{
    $("#txtHint").load("getuser.php?q="+str");
}
</script>
Recuerda que para utilizar jquery, tienes que incluir dicho archivo js en la cabecera... algo como:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

Coméntanos si te sirve, ok?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar