繁体   English   中英

使用Jquery Ajax POST加载PHP响应

[英]Loading PHP Response with Jquery Ajax POST

我有一个php脚本,可以遍历数据库查询的结果并将其打印出来。 我正在尝试使用AJAX将它们加载到管理界面面板中,但无济于事。 我的工作要求我主要编写后端代码,因此我还没有去学习很多JS / Jquery。 无论如何,我有一个页面“ insert.php”,该页面上有一个我想单击的按钮,并让它调用“ posts.php”页面中的结果。 这是我的档案

insert.php

                            <a href="#" class="button expand" id="ajaxbtn">Load Posts</a>

                        </div>
                    </div>

                    <div class="row main">
                        <div class="small-8 columns" id="posts">
                        </div>
                    </div>
                </div>

posts.php

<?php

require_once 'connect.php';

$user = 'admin';

$sql = "SELECT * FROM posts WHERE author = ?";

$stmt = $db->prepare($sql);
$stmt->bindParam(1, $user);
$stmt->execute();

$data = $stmt->fetchAll();

foreach ($data as $row)
{   
    $id = $row['id'];
    $title = $row['title'];
    $author = $row['author'];
    $date = $row['date'];
    $smalltext = $row['smalltext'];
    $bodytext = $row['bodytext'];
    $images = $row['images'];
    $imagelist = split(' ', $images);

    $shorttext = str_replace(
        array("\r\n", "\n"), 
        array("</p><p>", "</p><p>"), 
        $smalltext);

    echo 
    "
    <div class='row main'>
        <h1 class='padding-top-12 bottom-rule-green'>$title</h1>
        <div class='small-2 columns'>
            <p class='text-justify small-text'>
                Post MetaData goes here
            </p>
        </div>

        <div class='small-10 columns bottom-rule-red text-justify'>
            <p>
                $shorttext

                ";

                foreach ($imagelist as $key => $value)
                {
                    echo "<img src='users/$author/img/$value'>";
                }
    echo 
                "
            </p>
        </div>
    </div>

    <div class='row main small-text padding-top-1'>
        <div class='small-2 small-oofset-2 columns'>
            <a href='posts/$author/$id'>Edit Post</a>
        </div>

        <div class='small-4 columns'>
            Timestamp: $date
        </div>

        <div class='small-4 columns'>
            Author: <a href='users/$user'>$user</a>
        </div>
    </div>
    ";
}



?>

postAjax.js

$.ajaxSetup ({
    cache: false
});

var loadUrl = "../../includes/posts.php";

$(function(){
  $('#ajaxbtn').on('click', function(e){
    e.preventDefault();
    $('#ajaxbtn').fadeOut(300);

    $.post(loadUrl, {language: "php", version: 5},
        function(res){
            $("posts").html(res)
        }, "html");


    });
});

这是将我的脚本加载到页面中的文件

<!--Foundation Framework Necessities-->
<script type="text/javascript" src="../../assets/js/vendor/jquery.js"></script>
<script type="text/javascript" src="../../assets/js/foundation/foundation.min.js"></script>
<script type="text/javascript" src="../../assets/js/postAjax.js"></script>
<script type="text/javascript">
    $(document).foundation();
</script>

当我单击#ajaxbtn按钮时,它会淡出,因此我知道JS正在对该元素onclick调用JS,但是它不会发布posts.php的结果。 我认为这可能只是我对Ajax的工作方式的误解; 请告诉我我做错了什么。

尝试改变,

$("posts").html(res)

 $("#posts").html(res)

另外,我在posts.php中的代码中看到一些错误,您没有将php变量正确地嵌入字符串中。

我相信您需要使用委托事件。 从以下位置更改代码:

$('#ajaxbtn')。on('click',function(e)

至:

$('#ajaxbtn')。on('click','a',function(e)

这样,即使该父元素的内容发生更改,事件处理程序也会按预期触发。 我有同样的问题,这解决了它。 祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM