1 pièce(s) jointe(s)
Ajouter des données à ma table en BdD
bonjour,première post ;
j'ai un problème au niveau de communication entre AJAX et PHP ...
voila mon code html :
formulaire inclue dans un pop-up et le button "ajouter" qui doit appeler la fonction JS "add_admin()".
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 30 31 32 33 34 35 36 37 38
| <!-- deb Modal add-->
<p id="result"></p>
<div id="userModal" class="modal fade">
<div class="modal-dialog">
<form method="get" id="user_form" >
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add User</h4>
</div>
<div class="modal-body">
<label>nom</label>
<input type="text" name="nom" id="nom" class="form-control" />
<br />
<label>prenom</label>
<input type="text" name="prenom" id="prenom" class="form-control" />
<br />
<label>email</label>
<input type="text" name="email" id="email" class="form-control" />
<br />
<label>login</label>
<input type="text" name="login" id="login" class="form-control" />
<br />
<label>password</label>
<input type="password" name="pwd" id="pwd" class="form-control" />
<br />
</div>
<div class="modal-footer">
<!-- <input type="hidden" name="user_id" id="user_id" />-->
<!-- <input type="hidden" name="operation" id="operation" />-->
<input type="button" id="send" value="ajouter" class="btn btn-success" onclick="add_admin()" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
</div>
<!-- fin Modal add--> |
et voila mon code js qui contient l'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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <script type="text/javascript">
//function add(){alert ("aaa");}
function getXhr()
{
var xhr = null;
try { xhr = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e1)
{
try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
catch (e2)
{
try { xhr = new XMLHttpRequest(); }
catch (e3)
{
alert("AJAX n'est pas supporté par votre navigateur");
return false;
}
}
}
return xhr;
}
function add_admin()
{
var nom = document.getElementById('nom').value;
var prenom = document.getElementById('prenom').value;
var email = document.getElementById('email').value;
var prenom = document.getElementById('login').value;
var pwd = document.getElementById('pwd').value;
var params += '?nom=' + encodeURIComponent(nom);
params += '&prenom=' + encodeURIComponent(prenom);
params += '&email=' + encodeURIComponent(email);
params += '&login=' + encodeURIComponent(login);
params += '&pwd=' + encodeURIComponent(pwd);
var xhr1 = getXhr();
xhr1.onreadystatechange = function()
{
if (xhr1.readyState == 4 && xhr1.status == 200)
document.getElementById("result").innerHTML = xhr1.responseText;
var url = 'add_traitement.php' + params;
xhr1.open('GET', url, true);
xhr1.send();
}
</script> |
et 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 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?php
session_start();
require_once("cnx.php");
if(!isset($_SESSION['usser']))
// header("location:index.php");
echo "soyez connecte d'abord !!";
?>
<?php
$n=$_GET["nom"];
$p=$_GET["prenom"];
$e=$_GET["email"];
$l=$_GET["login"];
$pwd=$_GET["pwd"];
if(empty($n) and empty($p) and empty($e) and empty($l) and empty($pwd))
{
echo "tt champs obligatoirs/manquant";
}
else
{
$req="insert into personne (nom,prenom,email,login,pwd) values(?,?,?,?,?)";
$res = $bdd->prepare($req);
$res->bindValue(1,$n, PDO::PARAM_STR);
$res->bindValue(2,$p, PDO::PARAM_STR);
$res->bindValue(3,$e, PDO::PARAM_STR);
$res->bindValue(4,$l, PDO::PARAM_STR);
$res->bindValue(5,$pwd,PDO::PARAM_STR);
$res->execute();
if($res) {
// header("location:table-data.php");
echo "bien";
}
else{ echo "2:ps bien error dans: $req";}
}
?> |