[英]Sending post request using ajax and load the data into the same php file
[英]How can i load php file after sending ajax data
所以我有2个文件file1.php和所有php和ajax,以及file2.php和仅php。
$_POST["there_id"]
将通过ajax从file1.php页面发送到file2.php。
file1.php代码:
<div class="list_items">
<a href="" class="box" id="56545"><li><p>content here ...</p></a>
</div>
<div class="content_area">
//load ajax content here
</div>
$(".box").on("click", function(e) {
e.preventDefault();
var there_id = $(this).attr("id");
$.ajax({
url: "indexnew.php",
type: "POST",
data: { there_id : there_id}
});
});
file2.php代码:
<div id="file2content>
<?php
$there_id = $_POST['there_id'];
$user_id = 5;
$fetch_data = regular_query("SELECT * FROM contents WHERE
(user_by = :me or user_by + :them) AND (user_to = :me or user_to = :them)",
["me" => $user_id, "them" = $there_id], $conn);
foreach ($fetch_data as $data) : ?>
<a href=""><li><p>database data here</p></a>
<?php
endforeach;
?>
</div>
现在我想做的是用PHP完成file2.php并列出项目可用时,我想将file2contents
加载到file2contents
上的content_area
上。
$(".box").on("click", function(e) {
e.preventDefault();
var there_id = $(this).attr("id");
$.ajax({
url: "indexnew.php",
type: "POST",
data: { there_id : there_id},
success: function(data) {
$('.content_area').html(data);
}
});
});
但是, content_area
实际上应该是一个id而不是一个类,以便它是唯一的。
由于您只是返回html,因此可以使用.load()
函数对其进行简化。
$('.box').on('click', function(e) {
e.preventDefault();
var there_id = $(this).attr('id');
$('.content_area').load('indexnew.php', {there_id: there_id});
});
success: function(data) { $(".content_area").html(data); }
如果我对您的理解正确,那么将以上内容添加到您的AJAX通话中应该可以完成您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.