PHPからAjaxを使用して、入力から取得した値をphpmyadminのテーブルに挿入しています。私はキャンプで働いていますが、私は入力の価値を得ることに問題があります。ここでAjax/PHP:データベースの更新
は私のHTMLです:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>inscription client</title>
<script >
function test() {
r=document.getElementById('1');
if (r.value.length !==9)
document.getElementById('demo').innerHTML="verifer le mot de passe";
else document.getElementById('demo').innerHTML="";
}
//////////////////////////////////////////
function ajouter()
{
var cin=document.getElementById("cin").value;
var nom=document.getElementById("nom").value;
var mot_de_passe=document.getElementById("mot_de_passe").value;
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost/amir/inscrireClient.php?cin="+cin+"&nom="+nom+"&mot_de_passe="+mot_de_passe;
xmlhttp.onreadystatechange=function() {
if (this.readyState == 4 && this.status == 200)
{
if(this.responseText =="ok")
{
document.getElementById("2").innerHTML ="it woeks !";
document.getElementById("2").style.backgroundColor="green";
}
else{
document.getElementById("2").innerHTML="no";
document.getElementById("2").style.backgroundColor="red";
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
</script>
</head>
<body>
<input id ="cin" type="number" name="cin" value="123654789" onblur="test()" required >
<p style="color: red" id="demo"></p>
<input type="text" id="nom" name="nom" value="aaa" placeholder="donner votre nom" > <br> <br>
<input type="password" id="mot_de_passe" name="mot_de_passe" value="aaa" placeholder="donner votre mot de passe" required=> <br> <br>
<button type="submit" onclick="ajouter()">s'inscrire </button>
<p id="2" ></p>
</body>
</html>
そして、このPHPファイル:
<?php
include 'param.php';
header("Access-Control-Allow-Origin: *");
$cin=$_GET['cin'];
$nom=$_GET['nom'];
$mot_de_passe=$_GET['mot_de_passe'];
try
{
$bdd = new PDO('mysql:host='.$server.';dbname='.$database.';charset=utf8', $user, $passwd);
$bdd->exec("SET CHARACTER SET utf8");
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$reponse = $bdd->exec("insert into client(cin,nom,mot_de_passe) values ($cin,'$nom','$mot_de_passe')");
if ($reponse->rowCount()>0)
echo "ok";
else echo "non";
?>
これは、データベースに変更されません:
'id'には入力要素がありません: 'var cin = document.getElementById( "cin")。value; '。 'id'ではなく 'name'を設定しました – Jeff
クエリテキストに構文エラーがあります。 –
私はIDを変更しましたが、まだ動作しません –