使用AJAX从PHP页面回显整个帖子

时间:2016-07-16 04:15:23

标签: php jquery ajax

我已经使用Ajax PHP创建了一个搜索。但是当我试图在我的搜索页面中从PHP脚本回显整个帖子时,一切都很顺利。它不起作用。在PHP中编写PHP脚本显示所有帖子而不点击特定帖子的页面。 有没有办法在不包含PHP脚本的情况下使用ajax进行回显?

这是我的 search.php

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                $('#search').keyup(function(){
                    var sea = $('#search').val();
                    $.get('test.php',{getsearch:sea},function(data){
                        $('#result').show().html(data);
                    });
                });

                $('#search').blur(function(){
                    $('#result').hide();
                });

        });
    </script>
</head>
<body>
<div class="cover">
    <div class="btns-holder">
            <input type="search" name="s" placeholder="Search for something.." id="search" autocomplete="off" /><input type="submit" name="sub" value="" id="sub" />
    </div>
</div>



<div id="result">
</div>
<?php
$post = @$_GET['p'];
$que = "SELECT * FROM `search` WHERE `id`='$post'";
$run  = mysqli_query($con,$que);
    while($row=mysqli_fetch_array($con,$que)):

?>
    <img src="<?php echo $row['image']; ?>" widrh="500px" height="450px" style="float: left;"></img>
    <h2 style="font-family: sans-serif;"><?php echo $row['title']; ?></h2>
<?php endwhile; ?>
</body>

这是我想要回显数据的 test.php

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "ajax_search";
    $con  = mysqli_connect($host, $user, $pass, $db);
    if(mysqli_connect_errno())
    {
        echo "connection failed".mysqli_connect_error();
    }

$search = @$_GET['getsearch'];
$que = "SELECT * FROM `search` WHERE `title` LIKE '%$search%'";
$run = mysqli_query($con,$que);

    while($row=mysqli_fetch_assoc($run)):
?>

    <a href="search.php?p=<?php echo $row['id']; ?>">
        <div class="container">
            <img src="<?php echo $row['image']; ?>" width="50px" height="45px"></img>
            <h3><?php echo $row['title']; ?></h3></br>
        </div>
    </a>
<?php endwhile; ?>

0 个答案:

没有答案