不知道如何使用 ajax 将 html 输出到页面上


Do not know how to output html onto a page with ajax

我跟着看了一个教程视频(长度为112个视频),所以在编码方面,我仍然在很多方面都很无知;尽管我已经学到了很多东西。

我遇到的困难只是标题所说的。在我目前正在处理的管理面板中,我(例如)将 jquery 引导程序帮助程序类分配给一个段落标签"警报警报成功",如下所示:http://i1379.photobucket.com/albums/...ps44962161.png

它在上面的例子中工作的原因是因为在点击保存(插入和更新)时运行的 SQL 不需要 ajax。

插入和更新时执行的代码位于配置文件夹中名为"query.php"的文件中。它如下(当然是错误处理部分):

case 'pages':
if (isset($_POST['submitted']) && $_POST['submitted'] == 1) {
// POST vars    
$title = mysqli_real_escape_string($dbc, $_POST['title']);
$label = mysqli_real_escape_string($dbc, $_POST['label']);
$header = mysqli_real_escape_string($dbc, $_POST['header']);
$body = mysqli_real_escape_string($dbc, $_POST['body']);
if (isset($_POST['id']) AND $_POST['id'] != ''){
$action = "updated";    
// UPDATE   
$query = "UPDATE posts SET user = $_POST[user], slug = '$_POST[slug]', title = '$title', label =     '$_POST[label]', header = '$header', body = '$body' WHERE id = $_GET[id]"; 
}else{
$action = "added";  
// INSERT 
$query = "INSERT INTO posts (type, user, slug, title, label, header, body) VALUES (1, $_POST[user],  '$_POST[slug]', '$title','$label','$header','$body')";
}
$result = mysqli_query($dbc, $query);
//Error Handling    
if ($result) {
$message = '<p class = "alert alert-success">Page was '.$action.'!</p>';
}else{
$message = '<p class = "alert alert-danger">Page could not be '.$action.' because:         '.mysqli_error($dbc).'</p>';
$message .= '<p class = "alert alert-warning">Query: '.$query.'</p>'; 
}   
}
if (isset($_GET['id'])) {$opened = data_post($dbc, $_GET['id']);} 
break;

但是,我不知道如何在删除帖子时回显$message变量。我正在使用 ajax,以便帖子立即从左侧列表中消失。

这是Javascript:

$(".page-delete").on("click", function(){
var selected = $(this).attr("id");
var page_id = selected.split("del_").join("");
var confirmed = confirm("Are you sure you wanted to DELETE this page?");
if (confirmed == true) {
$.get("ajax/pages.php?id="+page_id); 
$("#page_"+page_id).remove();
};

。这是 Ajax:

include '../../config/connection.php';
$id = $_GET['id'];
$query = "DELETE FROM posts WHERE id = $id";
$result = mysqli_query($dbc, $query);
if ($result) {
echo "Page Deleted.";
}   else {
"There was an error...<br>";
echo $query.'<br>';
echo mysqli_error($dbc);
}

这是我试图吐出消息的地方:

</div>  <!-- END col-md-3 -->
<div class="col-md-9">
<!--FORM--> 
<form role="form" action="index.php?page=pages&id=<?php echo $opened['id']; ?>" method="post">
<?php if(isset($message)) { echo $message; } ?>
<!-- INPUT FIELD for title -->  
<div class="form-group">
<label for="title">Page Title</label>
<input type="text" class="form-control" value="<?php echo $opened['title'];?>" name="title"         id="title" placeholder="Page's Title" />
</div>

感谢任何可以提供帮助的人,我很抱歉不知道一些可能基本的东西。

再次感谢您的阅读。和平!

你需要对ajax get有一个回调。

$.get("ajax/pages.php?id="+page_id, function( data ) {
    $( "#result" ).html( data );  // replaces the html in div#result
    $("#page_"+page_id).remove();
 })

并将您的信息放在 html 标签中,例如 div,以便我们可以将其替换为 jquery

<div id="result"><?php if(isset($message)) { echo $message; } ?></div>

你的问题有点到处都是,你可能想阅读一些关于最佳实践的书。