timmah1 Posted November 18, 2008 Share Posted November 18, 2008 I'm trying to make sure password fields match before submitting, and no matter what I have done here, it always says "Passwords do not match" What am I doing wrong? <script type="text/javascript"> function toggle_password(password) { if (window.XMLHttpRequest) { http = new XMLHttpRequest(); } else if (window.ActiveXObject) { http = new ActiveXObject("Microsoft.XMLHTTP"); } handle = document.getElementById(password); var url = 'ajax1.php?'; if(handle.value.length > 0) { var fullurl = url + 'do=check_password_exists&password=' + encodeURIComponent(handle.value); http.open("GET", fullurl, true); http.send(null); http.onreadystatechange = statechange_password; }else{ document.getElementById('password_exists').innerHTML = ''; } } function statechange_password() { if (http.readyState == 4) { var xmlObj = http.responseXML; var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data; document.getElementById('password_exists').innerHTML = html; } } </script> Now the ajax1.php page $do = $_GET['do']; switch($do) { case 'check_password_exists': if(get_magic_quotes_gpc()) { $pass = $_GET['password']; $pass1 = $_GET['password1']; }else{ $pass = addslashes($_GET['password']); $pass1 = addslashes($_GET['password1']); } header('Content-Type: text/xml'); header('Pragma: no-cache'); echo '<?xml version="1.0" encoding="UTF-8"?>'; echo '<result>'; if($pass == $pass1) { echo 'Password match'; } else { echo 'Passwords do not match'; } echo '</result>'; break; default: echo 'Error, invalid action'; break; } Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/133219-solved-password-validation/ Share on other sites More sharing options...
gevans Posted November 18, 2008 Share Posted November 18, 2008 Correct me if I'm wrong, but you're only sending one of the password fields to the script, $pass1 is always empty because you haven't sent it in the JavaScript URL. Quote Link to comment https://forums.phpfreaks.com/topic/133219-solved-password-validation/#findComment-692876 Share on other sites More sharing options...
timmah1 Posted November 18, 2008 Author Share Posted November 18, 2008 You were right. I'm pretty sure this isn't the correct way because now all it shows is 'Passwords Match' <script type="text/javascript"> function toggle_password(password) { if (window.XMLHttpRequest) { http = new XMLHttpRequest(); } else if (window.ActiveXObject) { http = new ActiveXObject("Microsoft.XMLHTTP"); } handle = document.getElementById(password,password1); var url = 'ajax1.php?'; if(handle.value.length > 0) { var fullurl = url + 'do=check_password_exists&password' + encodeURIComponent(handle.value); var fullurl = url + 'do=check_password_exists&password1' + encodeURIComponent(handle.value); http.open("GET", fullurl, true); http.send(null); http.onreadystatechange = statechange_password; }else{ document.getElementById('password_exists').innerHTML = ''; } } function statechange_password() { if (http.readyState == 4) { var xmlObj = http.responseXML; var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data; document.getElementById('password_exists').innerHTML = html; } } </script> Have any idea on how to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/133219-solved-password-validation/#findComment-692879 Share on other sites More sharing options...
popcornplya Posted November 18, 2008 Share Posted November 18, 2008 Just do <script type="text/javascript"> function check() { var pass1 = document.getElementById("pass1").value; var pass2 = document.getElementById("pass2").value; if(pass1 == pass2) { return true; } else { return false; } } </script> then set id's for each password field and.. onsubmit="check()" for your form Quote Link to comment https://forums.phpfreaks.com/topic/133219-solved-password-validation/#findComment-692942 Share on other sites More sharing options...
timmah1 Posted November 18, 2008 Author Share Posted November 18, 2008 Not really what I wanted to accomplish, but it'll have to do for now. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/133219-solved-password-validation/#findComment-692957 Share on other sites More sharing options...
KevinM1 Posted November 18, 2008 Share Posted November 18, 2008 You were right. I'm pretty sure this isn't the correct way because now all it shows is 'Passwords Match' <script type="text/javascript"> function toggle_password(password) { if (window.XMLHttpRequest) { http = new XMLHttpRequest(); } else if (window.ActiveXObject) { http = new ActiveXObject("Microsoft.XMLHTTP"); } handle = document.getElementById(password,password1); var url = 'ajax1.php?'; if(handle.value.length > 0) { var fullurl = url + 'do=check_password_exists&password' + encodeURIComponent(handle.value); var fullurl = url + 'do=check_password_exists&password1' + encodeURIComponent(handle.value); http.open("GET", fullurl, true); http.send(null); http.onreadystatechange = statechange_password; }else{ document.getElementById('password_exists').innerHTML = ''; } } function statechange_password() { if (http.readyState == 4) { var xmlObj = http.responseXML; var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data; document.getElementById('password_exists').innerHTML = html; } } </script> Have any idea on how to fix this? It looks like you're rushing your code without really thinking about what you're adding. So, let's take a step back and look at this objectively. You have two password fields. You need to send each of them to the underlying PHP script. The first step is to obtain the value from each field: var pass = document.getElementById('password'); var passCheck = document.getElementById('password1'); Okay, now, you need to send them along: if(pass.value.length > 0 && passCheck.value.length > 0) { var fullUrl = url + 'do=check_password_exists&password=' + encodeURIComponent(pass.value) + '&password1=' + encodeURIComponent(passCheck.value); /* continue like normal from here */ } That should be all you need to change. Just remember to think about each component individually. In your first attempt, you were only sending one password to the PHP script. In your second attempt, you were sending the value from the first password field twice. Quote Link to comment https://forums.phpfreaks.com/topic/133219-solved-password-validation/#findComment-692986 Share on other sites More sharing options...
timmah1 Posted November 19, 2008 Author Share Posted November 19, 2008 Thank you for that Now I don't any kind of results <script type="text/javascript"> function toggle_password(password) { if (window.XMLHttpRequest) { http = new XMLHttpRequest(); } else if (window.ActiveXObject) { http = new ActiveXObject("Microsoft.XMLHTTP"); } //handle = document.getElementById(password); var pass = document.getElementById('password'); var passCheck = document.getElementById('password1'); var url = 'ajax1.php?'; if(pass.value.length > 0 && passCheck.value.length > 0) { var fullUrl = url + 'do=check_password_exists&password=' + encodeURIComponent(pass.value) + '&password1=' + encodeURIComponent(passCheck.value); http.open("GET", fullurl, true); http.send(null); http.onreadystatechange = statechange_password; }else{ document.getElementById('password_exists').innerHTML = ''; } } function statechange_password() { if (http.readyState == 4) { var xmlObj = http.responseXML; var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data; document.getElementById('password_exists').innerHTML = html; } } </script> Quote Link to comment https://forums.phpfreaks.com/topic/133219-solved-password-validation/#findComment-693111 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.