Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/06/2013, 05:19
rubengonzalez
 
Fecha de Ingreso: mayo-2008
Mensajes: 11
Antigüedad: 16 años
Puntos: 0
Validacion ajax

Hola, estoy intentando validar un formulario con ajax, en el formulario quiero que el número del ejemplar me lo valide en función de que si el isbn esta en la base de datos o no, estoy intentándolo hacer de la siguiente forma, pero no me lo valida bien, creo que será en petición_http.send que no me envía bien los valores al comprueba.php ¿Sabéis porque puede ser?
anadir.php
<form action="" method="post">
<fieldset class="input" style="border: 0 none;">
<p>
<label for="isbn">ISBN</label><br />
<input id="isbn" type="text" name="isbn" alt="isbn" style="width:40%" />
<div id="mensaje"></div>
</p>
<p>
<label for="numero">Número del ejemplar</label><br />
<input id="numero" type="text" name="numero" onkeyup="compruebaCampo2()" alt="numero" style="width:40%" />
<div id="respuesta"></div>
</p>
<p>
<br /><input type="submit" value="Añadir" name="enviar" class="art-button" />
</p>
</fieldset>

</form>
-------------------------------
scriptNum.js
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

var peticion_http;

function enviaPeticion2(url, metodo, funcion) {
peticion_http = inicializa_xhr2();
var num = "numero=" + document.getElementById("numero").value;
var is = "isbn=" + document.getElementById("isbn").value;
var query_string = "numero=" + encodeURIComponent(num) + "&isbn=" + encodeURIComponent(is);
if(peticion_http) {
peticion_http.onreadystatechange = funcion;
peticion_http.open(metodo, url, true);
peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
peticion_http.send(query_string);
}
}

function inicializa_xhr2() {
if(window.XMLHttpRequest) {
return new XMLHttpRequest();
}
else if(window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
}

function muestraResultado2() {
if(peticion_http.readyState == READY_STATE_COMPLETE) {
if(peticion_http.status == 200) {
if(peticion_http.responseText == '1')
{
document.getElementById("respuesta").innerHTML='<f ont color="#FF0000">¡Número ejemplar no disponible!</font>';
}
else
{
document.getElementById("respuesta").innerHTML='<f ont color="#009900">¡Número ejemplar disponible!</font>';
}
}
}
}

function compruebaCampo2() {
enviaPeticion2("comprobarCampoNum.php", "POST", muestraResultado2);
}
---------------------------------
comprobarCampoNum.php
if($_POST['numero']=='1' && $_POST['isbn']=='1'){
echo '1';
}else{
echo '0';
}