daniel__
daniel__

Reputation: 11845

ajax and php validation

my doubt is, when i put document.getElementById('myPassword').value); in the function validarDados it is correspondent to the valor, then i need another varriable, but the valor1 isn't work as i expect, the result in a echo (echo $valor1;) is undefined (echo $valor; works fine)

<input type="password"  id="myPassword"  name="password" class="text" onblur="validarDados('myPassword', document.getElementById('myPassword').value);" />

<input type="password" id="passwordMatch" name="passwordMatch" class="text"  onblur="validarDados('passwordMatch', document.getElementById('passwordMatch').value);" />

ajax

var req;

function validarDados(campo, valor, valor1) {
    if(window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    }

    else if(window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }


    var url = "ajax/validacao.php?campo="+campo+"&valor="+valor +"&valor1="+valor1;

    req.open("Get", url, true); 

    req.onreadystatechange = function() {

        if(req.readyState == 1) {
            document.getElementById('campo_' + campo + '').innerHTML = '<font color="gray">Verificando...</font>';
        }

        if(req.readyState == 4 && req.status == 200) {

            var resposta = req.responseText;

            document.getElementById('campo_'+ campo +'').innerHTML = resposta;
        }
    }

    req.send(null);
}

in a php file

$campo = $_GET['campo'];
$valor = $_GET['valor'];
$valor1 = $_GET['valor1'];

any ideia?

thanks

Upvotes: 0

Views: 183

Answers (2)

ace
ace

Reputation: 6825

You may try to use JQuery instead: http://api.jquery.com/jQuery.ajax

With regards to the code you've provided valor1 is already undefined before you submit it via ajax. Does the echo in your php printed undefined.

Try to alert the valor1 before passing it to ajax so you will see that it is undefined.

The reason why it is undefined because your onblur="(validarDados())" has only 2 parameters. To fix this add the third parameter like this:

<input type="password"  id="myPassword"  name="password" class="text" onblur="validarDados('myPassword', this.value,'your3rdparam');" />

Note: I used this.value instead of document.getElementById('myPassword').value to save space, does it has the same value base on the id you used.

You can also try to remove the third parameter valor1 in your function validarDados(). Then create a variable valor1 inside that method and specify its values. I wonder what is the purpose of valor1? anyway I hope this can be a help.

Upvotes: 1

Nuno
Nuno

Reputation: 3632

You should use Prototype instead: http://www.prototypejs.org/

And then, using Ajax.Request or Ajax.Updater, you just need to send to PHP the parameters "login" and "password".

new Ajax.Updater({success: "DIV ID HERE"}, "PROCESS.PHP", {
parameters: "login="+login+"&password="+password, method: "post"
});

As for your code, there is an error here:
<input type="password" id="myPassword" name="password" class="text" onblur="validarDados('myPassword', document.getElementById('myPassword').value);" />
It is missing the 3rd parameter "valor1".

Upvotes: 0

Related Questions