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

时间:2014-05-17 09:18:52

标签: javascript php jquery mysql ajax

enter image description here

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

我有三个涉及此功能的php页面,它们是' DB.php',' comment.php'和' action.php' 代码如下所示:

db.php中

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

comment.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>

当我运行这个时,当我插入评论时页面什么都没显示,有人可以帮我解决这个问题吗?非常感谢!!

1 个答案:

答案 0 :(得分:0)

进行了一些更改,如下所示:

comment.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>';
}
?>

您的代码失败的原因:

  • 未在dataString中添加名称,导致名称未在帖子中发送
  • 一些mysql错误