
Ajax no responde
Publicado por victor (29 intervenciones) el 26/05/2017 10:58:33
Buenos dias.
Estoy empezando con ajax y estoy haciendo una practica y no vá.
La cosa es que tengo que hacer una peticion ajax que me diga si el nombre de usuario existe o no al salir del input que contiene el nombre.
Este seria el html:
Y este el pequeño php
Alguna idea?
Estoy empezando con ajax y estoy haciendo una practica y no vá.
La cosa es que tengo que hacer una peticion ajax que me diga si el nombre de usuario existe o no al salir del input que contiene el nombre.
Este seria el html:
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Alta Usarios</title>
<script type="text/javascript">
function UsuarioExistente(nombre)
{
alert("Dentro de la funcion");
var xmlhttp;
var nombreRecibido = nombre;
var contenidoMostrar = "";
//Valor del label de respuesta
var valorLabelRespuesta = document.getElementById("UsuarioExiste");
var respuestaExisteMostrar = "";
//Si no se le ha pasado ningun valor
if (nombreRecibido.length == 0)
{
document.getElementById("UsuarioExiste").innerHTML = "";
return;
}
//alert("Linea 26");
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function ()
{
alert("Dentro de la funcion de respuesta");
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
nombreRecibido = xmlhttp.responseText.split;
for ($i = 0; $i < nombreRecibido.count; $i++)
{
alert("DentroDelfor"+nombreRecibido[i]);
}
}
}
var paramentros = 'nombreUsuario' + encodeURIComponent(nombreRecibido);
xmlhttp.open('POST', 'altaUsuarios.php'); // Método post y url invocada
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); // Establecer cabeceras de petición
xmlhttp.send(paramentros); // Envio de parámetros usando POST
}
</script>
</head>
<body>
<h1>Alta Usarios</h1>
<form enctype="application/x-www-form-urlencoded" method="post">
<label style="padding-right:20px;">Usuario</label>
<input name="nombreUsuario" onBlur="UsuarioExistente(this.value)"/>
<label name="UsuarioExiste"></label>
<br />
<label style="padding-right:15px;">Nombre</label>
<input name="nombre"/>
<br />
<label style="padding-right:5px;">Apellidos</label>
<input name="apellidos" />
<input type="submit" value="Enviar"/>
</form>
</body>
</html>
Y este el pequeño 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
<meta charset="utf-8"/>
<?php
//Nombres alojados
$nombres[0]="Jose";
$nombres[1]="Fernando";
$nombres[2]="Juan";
$msg="";
$nombreRecibido=$_POST["nombreUsuario"];
//echo"DentroDealtausuarios.php";
//exit;
$existeUsuario=false;
for($i=0;$i<$nombres.count();$i++)
{
if($nombres[$i]==$nombreRecibido)
{
$msg="El usuario ".$nombres[$i]."Ya existe";
$existeUsuario=true;
}else
{
$msg="Usuario ".$nombres[$i]."esta disponible";
}
}
echo $msg;
?>
Alguna idea?
Valora esta pregunta


0