Reputation: 9
the principle is that the php script verifies if the two passwords are the same , then it counts the number of character and then returns a message that the number .i'm sorry for my English I 'm French My html code
<fieldset class="appel">
<input id="premier_champ_password" type="password" />
<input id="second_champ_password" type="password" />
<input type="button" id="bouton_secrets" value="Vérifier les mots de passe"/>
<br />
<br />
<fieldset class="retour">
<legend>Contenu du fichier</legend>
<div id="retour_secrets"></div>
</fieldset>
</fieldset>
var bouton6=document.getElementById('bouton_secrets');
bouton5.addEventListener('click', appel_contenue_secret);
My ajax code
function appel_contenue_secret(){
var cM = new XMLHttpRequest();
cM.open("POST","fichiers/secrets.php",true);
cM.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var passeword = document.getElementById('premier_champ_password').value;
var verif_passeword = document.getElementById('second_champ_password').value;
var retour_solutions = document.getElementById('retour_solutions');
cM.send("passeword1"+passeword&"passeword2"+verif_passeword);
cM.onreadystatechange = function (){
if (cM.readyState == 4 && cM.status == 200)
{
retour_solutions.innerHTML=cM.responseText;
}
}
}
My PHP code
i want that the script : chek if the passeword are the same count the number of characters in the password return a message informing the difficulty of password according to the following principle in the output : id = retour_secret Between 1 and 6 characters : "Mot de passe faible" 7 to 12 characters :"Mot de passe moyen" From 13 characters : "Mot de passe complexe"
<?php
$passeword = trim($_POST["passeword1"]);
$verif_passeword =trim($_POST["passeword2"]);
if ($passeword==$verif_passeword)
{$nbre = strlen($passeword);
if (1 < $nbre < 7) echo "Mot de passe faible";
else if (7 < $nbre < 12) echo "Mot de passe moyen";
else if ($nbre > 12) echo "Mot de passe complexe";}?>
Upvotes: 1
Views: 77
Reputation: 5689
Try this.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Ajax Example</title>
<meta charset="utf-8">
<script>
function appel_contenue_secret(){
var cM = new XMLHttpRequest();
cM.open("POST","secrets.php",true);
cM.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var passeword = document.getElementById('premier_champ_password').value;
var verif_passeword = document.getElementById('second_champ_password').value;
var retour_solutions = document.getElementById('retour_solutions');
cM.send("passeword1="+passeword+"&passeword2="+verif_passeword);
cM.onreadystatechange = function (){
if (cM.readyState == 4 && cM.status == 200)
{
retour_solutions.innerHTML=cM.responseText;
}
}
}
</script>
</head>
<body>
<fieldset class="appel">
<input id="premier_champ_password" type="password" />
<input id="second_champ_password" type="password" />
<input type="button" id="bouton_secrets" value="Vérifier les mots de passe" onClick="appel_contenue_secret();"/>
<br />
<br />
<fieldset class="retour">
<legend>Contenu du fichier</legend>
<div id="retour_secrets"></div>
</fieldset>
<span id="retour_solutions"></span>
</fieldset>
</body>
</html>
PHP
<?php
$passeword = trim($_POST["passeword1"]);
$verif_passeword =trim($_POST["passeword2"]);
if ($passeword==$verif_passeword)
{
$nbre = strlen($passeword);
if ($nbre>=1 && $nbre<=7) echo "Mot de passe faible";
else if ($nbre>7 && $nbre<=12) echo "Mot de passe moyen";
else if ($nbre > 12) echo "Mot de passe complexe";
}
else
{
echo "Passowrds didn't match!";
}
?>
Note : I recommend you to use JavaScript library jQuery
to take advantage of built-in ajax functionalities than writing lengthy JavaScript Native Code.
Upvotes: 1
Reputation: 91213
I'm not sure what isn't working for your code, as you haven't specified the problem you are having, but two general practice considerations for you:
Use a PHP Framework. I would highly recommend Laravel. It's powerful and has an easy learning curve. It has built-in password confirmation validations for forms. http://www.laravel.com
I would suggest using jQuery for sending Ajax requests. The syntax is easy to learn and it makes the process dead simple. http://www.jquery.com
Using these two things will considerably decrease bugs for your code since they do a lot of the work for you.
Upvotes: 0