[AJAX] Enregistrement dans la base
salut , j'ai fait comme exemple 3 champs : nom, prénom et compte, après la récupération de ces champs , j'envoi les données avec $.post vers la page d'insertion, ça me semble un peut bizarre car parfois l'enregistrement se fait , et parfois non.
voici mon code html:
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 49 50 51 52
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document sans titre</title>
<link rel="stylesheet" href="style.css"/>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$('#submit').click(function(){
var link = 'insert.php';
var nom = $('#nom').val();
var prenom = $('#prenom').val();
var compte = $('#compte').val();
$.post(link,{nom:nom , prenom:prenom , compte:compte});
})
})
</script>
</head>
<body>
<form name="f" id="formulaires">
<div>
<label for="nom" >Nom</label>
<input type="text" id="nom"/>
</div>
<div>
<label for="prenom" >Prénom</label>
<input type="text" id="prenom"/>
</div>
<div>
<label for="compte" >Compte</label>
<input type="text" id="compte" />
</div>
<div>
<input type="submit" value="Envoyer" id="submit" class="submit"/>
</div>
</form>
</body>
</html> |
et voici insert.php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?php
require('connect.php');
if (!empty($_POST)){
extract($_POST);
$nom = strip_tags($nom);
$prenom = strip_tags($prenom);
$compte = strip_tags($compte);
$req = $bdd->prepare('INSERT INTO test(nom,prenom,compte) VALUES (:nom,:prenom,:compte)');
$req->execute(array(':nom'=>$nom,':prenom'=>$prenom,':compte'=>$compte));
$req->closeCursor();
}
?> |
merci pour votre aide