gpt4 book ai didi

javascript - 使用 ajax 从 mysql 添加和检索记录

转载 作者:行者123 更新时间:2023-11-30 23:01:40 25 4
gpt4 key购买 nike

enter image description here

如图所示,我的 mysql 中有两个表,我希望系统在不刷新页面的情况下添加和检索评论。

我在这个函数中涉及三个 php 页面,它们是“DB.php”、“comment.php”和“action.php”代码如图所示:

数据库.php

    <?php
$conn = mysql_connect('localhost','Practical4','1234') or die (mysql_error);
$db=mysql_select_db('Practical4', $conn) or die (mysql_error);
?>

评论.php

           <----------------ajax script-------------------->

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(".submit_button").click(function() {
var textcontent = $("#content").val();
var dataString = 'content='+ textcontent;
if(textcontent=='')
{
alert("Enter some text..");
$("#content").focus();
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<span class="load">Loading..</span>');
$.ajax({
type: "POST",
url: "action.php",
data: dataString,
cache: true,
success: function(html){
$("#show").after(html);
document.getElementById('content').value='';
$("#flash").hide();
$("#content").focus();
}
});
}
return false;
});
});
</script>
<div>

<-----retrieve hotel id from hotel table-------->
<?php
$conn=mysqli_connect('localhost','Practical4','1234') or die('Not connected');

$database=mysqli_select_db($conn,'Practical4') or die('Database Not connected');

$id=$_GET['id'];

$query = "select * from hotel where name='$id'";
$data=mysqli_query($conn,$query);
while($rows=mysqli_fetch_array($data)){

$name=$rows['name'];
$price=$rows['price'];
$duetime=$rows['dueTime'];
$address=$rows['location'];
}
?>

<---------------post form------------------->
<form method="post" name="form" action="">
<h3>Add Comment for <?php echo $name;?><h3>
<input type="text" name="name" id="name" value="<?php echo $name;?>" hidden > <br>
<textarea cols="30" rows="2" name="content" id="content" maxlength="145" >
</textarea><br />
<input type="submit" value="Post" name="submit" class="submit_button"/>

</form>
</div>
<div class="space"></div>
<div id="flash"></div>
<div id="show"></div>

Action .php

    <?php
include('DB.php');
$check = mysql_query("SELECT * FROM comment order by commentID desc");
if(isset($_POST['content']))
{
$content=mysql_real_escape_string(trim($_POST['content']));
$name=mysql_real_escape_string(trim($_POST['name']));

mysql_query("insert into comment(content,name) values ('$content','$name')");
$fetch= mysql_query("SELECT content FROM comment order by commentID desc where name = '$name'");
$row=mysql_fetch_array($fetch);
}
?>

<div class="showbox"> <?php echo $row['content']; ?> </div>

当我运行这个程序时,当我插入评论时页面没有任何显示,谁能帮我解决这个问题?非常感谢!!

最佳答案

部分改动如下:

评论.php

          <!-- ajax script -->

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(".submit_button").click(function() {
var textcontent = $("#content").val();
var name = $("#name").val();
var dataString = 'content='+ textcontent + '&name='+name;
if(textcontent=='')
{
alert("Enter some text..");
$("#content").focus();
}
else
{
$("#flash").show();
$("#flash").fadeIn(400).html('<span class="load">Loading..</span>');
$.ajax({
type: "POST",
url: "action.php",
data: dataString,
cache: true,
success: function(html){
$("#show").after(html);
document.getElementById('content').value='';
$("#flash").hide();
$("#content").focus();
}
});
}
return false;
});
});
</script>
<div>

<!-- retrieve hotel id from hotel table -->
<?php
include('DB.php');

$id=$_GET['id'];

$query = mysql_query("select * from hotel where name='$id'");
while($rows=mysql_fetch_array($query)){

$name=$rows['name'];
$price=$rows['price'];
$duetime=$rows['dueTime'];
$address=$rows['location'];
}
?>

<!-- post form -->
<form method="post" name="form" action="">
<h3>Add Comment for <?php echo $name;?><h3>
<input type="text" name="name" id="name" value="<?php echo $name;?>" hidden > <br>
<textarea cols="30" rows="2" name="content" id="content" maxlength="145" >
</textarea><br />
<input type="submit" value="Post" name="submit" class="submit_button"/>

</form>
</div>
<div class="space"></div>
<div id="flash"></div>
<div id="show"></div>

Action .php

<?php
include('DB.php');
$check = mysql_query("SELECT * FROM comment order by commentID desc");
if(isset($_POST['content']))
{
$content=$_POST['content'];
$name=$_POST['name'];

mysql_query("insert into comment (content,name) values ('$content','$name')");
echo '<div class="showbox">'.$content.'</div>';
}
?>

您的代码失败的原因:

  • name 未添加到 dataString 中导致 name 未在 post 中发送
  • 一些 mysql 错误

关于javascript - 使用 ajax 从 mysql 添加和检索记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23709639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com