在没有刷新的情况下发布数据+调用另一个jquery函数

时间:2017-03-02 01:40:51

标签: javascript php jquery sql ajax

我的代码,基本上,只需单击一个按钮,就会运行一个ajax函数,以便将东西写入我的数据库。

接下来我要做的是调用另一个从数据库中获取数据并打印它的函数。

下面是我的代码,但第二个函数没有显示它的工作原理。我不知道哪里出错了。

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
    function loaddata() {
        $.ajax({
            type: "POST",
            url: "includes/fetchupdatedimages.php",
            data: $("#editad_form").serialize(),
            success: function (response) {
                alert(response);
            }
        });
    });
</script>

第二功能:

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
    $(function () {
        $("#deleteimgs").click(function(event) {
            event.preventDefault();

            $.ajax({
                type: "POST",
                url: "includes/deleteimages.php",
                data: $("#editad_form").serialize()
            });

            $("input[type=checkbox]:checked").parent().remove();
            loaddata();
        });
    });
</script>

fetchupdatedimages.php

<?php
    include_once "functions.php";

    ini_set("display_errors", 1);
    ini_set("display_startup_errors", 1);
    error_reporting(-1);
    error_reporting(E_ALL);

    $id = $_POST["id"];
    if ($stmt = $mysqli->prepare("SELECT images FROM db WHERE id = ? LIMIT 1")) {
        $stmt->bind_param("s", $id);
        $stmt->execute();
        $stmt->store_result();

        // get variables from result.
        $stmt->bind_result($images);
        $stmt->fetch();
    }

    echo "<p>" . $images . "</p>";
 ?>

似乎loaddata()没有被调用,或者它没有向我返回任何数据。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试使用PHPJSON文件中的数据发送到jQuery代码?

例如:

PHP

<?php
    header("Content-Type: application/json");
    include 'connect.php';
    $sql = "SELECT * FROM reviews, customers WHERE review_user = customer_id";
    $datas = "";
    $x = 0;
    $result = $con->query($sql);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $datas[$x] = array("fname" => $row["customer_name"], "lname" => $row["customer_surname"], "email" => $row["customer_email"], "gender" => $row["customer_gender"], "title" => $row["review_title"], "content" => $row["review_content"], "rating" => $row["review_rating"]);
            $x++;
        }
    }
    $con->close();
    echo json_encode($datas);
?>

的jQuery

$(document).ready(function() {
    $.getJSON('controls/getReviews.php', function(jsondata) {
        console.log("Returned data: " + jsondata);
        if (jsondata !== "") {
            for (var i = 0; i < jsondata.length; i++) {
                var data = jsondata[i];
                var fname = data["fname"];
                var lname = data["lname"];
                var email = data["email"];
                var gender = data["gender"];
                var title = data["title"];
                var msg = data["content"];
                var rating = data["rating"];
                $('.reviews').append('<div class="panel panel-default"><div class="panel-heading"><h3 class="panel-title">' + title + '</h3></div><div class="panel-body"><table class="table table-striped"><tr><td>Name:</td><td>' + fname + ' ' + lname + '</td></tr><tr><td>Gender:</td><td>' + gender + '</td></tr><tr><td>Rating:</td><td>' + rating + '/5</td></tr><tr><td>Message:</td><td>' + msg + '</td></tr></table></div></div>');
            }
        }
    });
});