Php 当另一个AJAX函数成功时,我是否可以自动运行AJAX函数?
我将要用过程化PHP和AJAX制作一个注释系统,它将允许我在不刷新的情况下显示新注释,并在不刷新的情况下加载更多注释 在我目前的情况下,两者都起作用,但不能同时起作用。如果数据库中没有要显示的注释,则我的站点将显示一条文本,说明“没有要显示的注释”。如果我做了笔记,它仍然不会显示笔记,直到我通过点击载入笔记按钮载入它 我已尝试在成功函数中添加以下内容:Php 当另一个AJAX函数成功时,我是否可以自动运行AJAX函数?,php,jquery,ajax,Php,Jquery,Ajax,我将要用过程化PHP和AJAX制作一个注释系统,它将允许我在不刷新的情况下显示新注释,并在不刷新的情况下加载更多注释 在我目前的情况下,两者都起作用,但不能同时起作用。如果数据库中没有要显示的注释,则我的站点将显示一条文本,说明“没有要显示的注释”。如果我做了笔记,它仍然不会显示笔记,直到我通过点击载入笔记按钮载入它 我已尝试在成功函数中添加以下内容: if(res.indexOf("success")!=-1) { location.reload(true); } 没有任何运气,就连
if(res.indexOf("success")!=-1) {
location.reload(true);
}
没有任何运气,就连我也能在互联网上读到,它不知怎么地对其他人有用
以下是我的ajax功能:
$(document).ready(function() {
var noteCount = 2;
$("#loadMore").click(function() {
noteCount = noteCount + 2;
$("#notes").load("load-notes.php", {
noteNewCount: noteCount
});
});
$("#noteform").on("submit", function(event) {
event.preventDefault();
noteCount = noteCount + 1;
var form_data = $(this).serialize();
$.ajax({
url: "add-notes.php",
method: "POST",
noteNewCount: noteCount,
data: form_data,
dataType: "JSON",
success: function(data) {
if(res.indexOf("success")!=-1) {
location.reload(true);
}
}
});
});
});
myadd-notes.php
<?php
session_start();
require_once "../inc/core/config.php";
if ($_SERVER['REQUEST_METHOD'] != 'POST') exit;
$subject = mysqli_real_escape_string($dbconn, $_POST['subject']);
$note = mysqli_real_escape_string($dbconn, $_POST['note']);
$my_date = date("Y-m-d H:i:s");
$author = $_SESSION['u_firstname'];
$noteNewCount = $_POST['noteNewCount'];
$sql = "INSERT INTO notes(author, subject, note, created_at) VALUES ('$author', '$subject', '$note', '$my_date')";
mysqli_query($dbconn, $sql);
$sql = "SELECT * FROM notes LIMIT $noteNewCount";
$result = mysqli_query($dbconn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<div class="note">
<div class="noteHead">
<div class="row">
<div class="col-md-8">
<h3>' . $row["subject"] . '</h3>
</div>
<div class="col-md-4">
<p class="text-muted">Note created by ' . $row["author"] . '</p>
</div>
</div>
</div>
<div class="noteContent">
<div class="row">
<div class="col-12">
<p>' . $row["note"] . '</p>
</div>
</div>
</div>
<div class="noteFooter">
<div class="row">
<div class="col-12">
<p class="text-muted">Created ' . $row["created_at"] . '</p>
</div>
</div>
</div>
</div>';
}
} else {
echo "No comments yet...";
}
而不是location.reload
,只需替换#notes
中的HTML即可。另外,在addnotes.php
中,您是在回显HTML,但在ajax中,您需要dataType:json
。另外,不确定传递给AJAX的noteNewCount
是什么
$.ajax({
url: "add-notes.php",
method: "POST",
data: form_data,
success: function(data) {
$("#notes").html(data);
}
});
是的,你可以。你试过了吗?我试过什么?我使用过location.reload(true);重新加载页面并希望加载数据。没有成功。您是否尝试过通过将新函数放在第一个函数的回调函数中来调用另一个AJAX函数?
$.ajax({
url: "add-notes.php",
method: "POST",
data: form_data,
success: function(data) {
$("#notes").html(data);
}
});