Estou com um pequeno problema no meu código Ajax. O objetivo é avisar em um alert que o usuário já existe cadastrado.
Tenho o seguinte:
//form_validate.js
function validar(){
var usuario=document.frm.usuario.value;
var senha1=document.frm.pas1.value;
// Valida existência de usuário
var xmlhttp = false;
//Check if we are using IE.
try {
//If the javascript version is greater than 5.
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
//If not, then use the older active x object.
try {
//If we are using IE.
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
//Else we must be using a non-IE browser.
xmlhttp = false;
}
}
//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
var serverPage="users/veruser.php?us="+usuario;
var res;
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
res = xmlhttp.responseText;
}
}
xmlhttp.send(null);
if (res>=1){
alert("Este usuário já existe!");
document.frm.usuario.focus();
return false;
}
}
Esse acima é o código Ajax, e abaixo o código php referente ao arquivo veruser.php
// veruser.php
<?php
include "../access_client.inc";
$username=$_GET["us"];
$sql=mysql_query("SELECT username FROM psbd1_user_detail WHERE username='$username'");
$nl=mysql_num_rows($sql);
echo "$nl";
?>
Já executei o php individualmente e o resultado sai correto.
Ao submeter o formulário de cadastro o sistema deve verificar se o usuário já existe no banco de dados e se existir não submeter o formulário.
O código acima funciona e tal, porém só funciona no segundo submit, ou seja, no primeiro ele passa.
Pergunta
diogoleter
Prezados,
Estou com um pequeno problema no meu código Ajax. O objetivo é avisar em um alert que o usuário já existe cadastrado.
Tenho o seguinte:
//form_validate.js function validar(){ var usuario=document.frm.usuario.value; var senha1=document.frm.pas1.value; // Valida existência de usuário var xmlhttp = false; //Check if we are using IE. try { //If the javascript version is greater than 5. xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { //If not, then use the older active x object. try { //If we are using IE. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { //Else we must be using a non-IE browser. xmlhttp = false; } } //If we are using a non-IE browser, create a javascript instance of the object. if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { xmlhttp = new XMLHttpRequest(); } var serverPage="users/veruser.php?us="+usuario; var res; xmlhttp.open("GET", serverPage); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { res = xmlhttp.responseText; } } xmlhttp.send(null); if (res>=1){ alert("Este usuário já existe!"); document.frm.usuario.focus(); return false; } }Esse acima é o código Ajax, e abaixo o código php referente ao arquivo veruser.php// veruser.php <?php include "../access_client.inc"; $username=$_GET["us"]; $sql=mysql_query("SELECT username FROM psbd1_user_detail WHERE username='$username'"); $nl=mysql_num_rows($sql); echo "$nl"; ?>Já executei o php individualmente e o resultado sai correto.
Ao submeter o formulário de cadastro o sistema deve verificar se o usuário já existe no banco de dados e se existir não submeter o formulário.
O código acima funciona e tal, porém só funciona no segundo submit, ou seja, no primeiro ele passa.
Obrigado!
Editado por diogoleterLink para o comentário
Compartilhar em outros sites
4 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.