Ajax/PHP:更新数据库
我使用Ajax和PHP将从输入中获取的值插入phpmyadmin中的表中。我与camp合作,但我在获取输入值时遇到了问题 这是我的HTML:Ajax/PHP:更新数据库,php,mysql,ajax,Php,Mysql,Ajax,我使用Ajax和PHP将从输入中获取的值插入phpmyadmin中的表中。我与camp合作,但我在获取输入值时遇到了问题 这是我的HTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>inscription client</title> <script > function test() {
<!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>
题字客户机
功能测试(){
r=document.getElementById('1');
如果(r.value.length!==9)
document.getElementById('demo').innerHTML=“verifer le mot de passe”;
else document.getElementById('demo').innerHTML=“”;
}
//////////////////////////////////////////
函数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();
变量url=”http://localhost/amir/inscrireClient.php?cin=“+cin+”&nom=“+nom+”&mot_de_passe=“+mot_de_passe;
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200)
{
如果(this.responseText==“ok”)
{
document.getElementById(“2”).innerHTML=“it woeks!”;
document.getElementById(“2”).style.backgroundColor=“绿色”;
}
否则{
document.getElementById(“2”).innerHTML=“否”;
document.getElementById(“2”).style.backgroundColor=“红色”;
}
};
open(“GET”,url,true);
xmlhttp.send();
}
s'inscrire
这是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";
?>
修复您的sql语句:
$reponse = $bdd->exec( "insert into client(cin,nom,mot_de_passe) values ($cin,'$nom','$mot_de_passe)'" );
“$mot_de_passe)”部分。在结束括号之前,应先加上单引号。并假设$cin是数字。修复sql语句:
$reponse = $bdd->exec( "insert into client(cin,nom,mot_de_passe) values ($cin,'$nom','$mot_de_passe)'" );
// $reponse = $bdd->exec( "insert into client(cin,nom,mot_de_passe) values ($cin,'$nom','$mot_de_passe')" );
$sql = "insert into client(cin, nom, mot_de_passe) values (?,?,?)";
$stmt = $bdd->prepare($sql);
$stmt->bindParam(1, $cin);
$stmt->bindParam(2, $nom);
$stmt->bindParam(3, $mot_de_passe);
$stmt->execute();
“$mot_de_passe)”部分。在结束括号之前,应先加上单引号。并假设$cin是数字。您没有具有该“id”的输入元素:var cin=document.getElementById(“cin”).value;
。您已将'name'设置为'id',并且查询文本中存在语法错误。我更改了id,但仍然无效您没有具有该'id'的输入元素:var cin=document.getElementById(“cin”).value;
。您已将“名称”设置为“id”,并且查询文本中存在语法错误。我更改了id,但它仍然不起作用。我更改了sql语句,但它不起作用。查看linkXMLHttpRequest上的图片无法加载。请求的资源上不存在“访问控制允许源代码”标头。因此,源代码“null”为not允许访问。您是否尝试过允许方法-尤其是GET在您的情况下:header('access-Control-Allow-methods','PUT,GET,POST,DELETE,OPTIONS'));并将头函数放在php脚本的顶部-在所有操作之前。我更改了sql语句,但它不起作用。请查看linkXMLHttpRequest无法加载的图片。请求的资源上不存在“Access Control Allow Origin”头。因此,不允许访问Origin“null”。您是否尝试过允许方法-e在您的情况下,特别是GET:header('Access-Control-Allow-Methods','PUT,GET,POST,DELETE,OPTIONS');并将标题函数放在php脚本的顶部-在所有内容之前。仅代码的答案可能不是最佳答案。您是否愿意解释代码的作用/它如何帮助解决问题?谢谢。仅代码的答案可能不是最佳答案。您是否愿意解释代码的作用/它如何帮助解决问题?谢谢。
// $reponse = $bdd->exec( "insert into client(cin,nom,mot_de_passe) values ($cin,'$nom','$mot_de_passe')" );
$sql = "insert into client(cin, nom, mot_de_passe) values (?,?,?)";
$stmt = $bdd->prepare($sql);
$stmt->bindParam(1, $cin);
$stmt->bindParam(2, $nom);
$stmt->bindParam(3, $mot_de_passe);
$stmt->execute();