[AJAX] Authentification avec ajax
Salue a tous,
Cela fait pratiquement deux semaines que j'essaye de créer un formulaire d'authentification à une base de données, mais je n'y arrive pas.
voici mon code php:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
<?php
try
{
$pass = $_REQUEST['pass'];
$user = $_REQUEST['user'];
$db = new PDO('mysql:host=localhost;dbname=dbmedia', 'root', '');
$str='SELECT * FROM `tb_user` WHERE ((`login`='.$pass.') AND (`Pword`='.$user.'))';
echo $str;
$retour = $db->query($str);
while($row = mysql_fetch_array($retour))
{
echo $row[0];
}
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
} |
code ajax:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
//Détection de l'objet xhr
xhr =null;
if(window.XMLHTTPRequest){
xhr = new XMLHTTPRequest();
} else if(window.ActiveXObject){
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//fonction qui sera exécutée après la clic sur le bouton valider
function validerCnx(user,pass)
{
xhr.onreadyStatechange = function ()
{
if((xhr.readyState==4) && (xhr.Status ==200))
{
var reponse = xhr.responseText;
if(document.getElementById("log_user").innerHTML = xmlhttp.responseText)
{
alert(reponse);
}
}
}
xhr.open("GET","parametre.php?user="+user+"&pass="+pass,true);
xhr.send();
} |
Code du formulaire :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<form name="log_user" method="post">
<table width="119%" border="0" align="center">
<tr >
<td colspan="2" align="center"></td>
<tr>
<td colspan="2" class="lienbleugras"> </td>
</tr>
<tr>
<td class="corps">Nom utilisateur </td>
<td><input name="login_user" type="text" id="login_user" /></td>
</tr>
<tr>
<td class="corps">Mot de passse </td>
<td><input name="password_user" type="password" id="password_user" /></td>
</tr>
<tr>
<td> </td>
<td>
<input name="connexion" value="log_user" type="hidden" >
<input type="submit" name="Submit" value="Valider" onclick="validerCnx(login_user.value, password_user.value);"></td>
</tr>
</table>
</form> |
honnêtement je débute avec ajax et php et je ne sais même plus comment continuer j'ai vraiment besoin de votre aide.
Merci d'avance.