jQuery:
$("#envoyer_comm").click(function(e){
comment_value = $("#text_commentaire").val();
$.get("set_commentaire.php", {idP:idPhoto, commentaire:comment_value});
param = "idP=" + idPhoto;
$("#les_commentaires").load("load_commentaire.php", param);
$("#text_commentaire").val("");
e.preventDefault();
});
set_commentaire.php:
<?php
session_start();
$bdd = new PDO('mysql:host=127.0.0.1;dbname=pure', 'root', 'Starwars');
$insert_com = $bdd->prepare("INSERT INTO commentaire (idUserCom, idPhoto, commentaireValue) VALUES (?,?,?)");
echo $_GET['idP'].$_GET['commentaire'];
$insert_com->execute(array(1,$_GET['idP'], $_GET['commentaire']));
?>
load_commentaire.php:
<?php
session_start();
$bdd = new PDO('mysql:host=127.0.0.1;dbname=pure', 'root', 'Starwars');
$req_commentaire = $bdd->prepare("SELECT * FROM commentaire WHERE idPhoto = ? ORDER BY idCom ASC");
$req_commentaire->execute(array($_GET['idP']));
?>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="style.css?v=1.1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<title>Pure</title>
</head>
<body>
<?php
while($resultat = $req_commentaire->fetch()){
$req_user = $bdd->prepare("SELECT * FROM utilisateur WHERE idUser = ?");
$req_user->execute(array($resultat['idUserCom']));
$userinfo = $req_user->fetch();
echo "<tr><td><div id=\"commentaire\"><label>".$userinfo['pseudo']."</label><div id=\"commentaire_value\">".$resultat['commentaireValue']."</div></div></td></tr>";
} ?>
</body>
</html>
envoyer_comm
只是一个按钮:
<button id="envoyer_comm">Envoyer</button>
我不希望我的网页在此功能后刷新。
e.preventDefault();
或
return false;
做好这份工作,但它把我带到了我的页面顶部,我希望它能留在我所在的地方
有什么想法吗?