【发布时间】:2020-11-23 06:58:58
【问题描述】:
所以它有点复杂,我会尽量让它简单。 我有一个有 3 列的表,我做了一个功能,如果你按下列,它会给你帖子的 ID。 在此之后我需要 id 以便我可以在新页面上显示帖子 - post.php。
我提出了这个要求
$(".click_title").click(function() {
var $item = $(this).closest("tr").find(".post_id_c").text();// for sure work
$.post("post.php",{"post_id_to_show":$item},function(){
location.href = "post.php";
});
});
在我开始的php上:
if (isset ($_POST["post_id_to_show"]) ){
$post_id_to_show = $_POST["post_id_to_show"];
}else{
echo "errror";
}
现在我从 echo 中得到一个错误。
然后我想显示 sql 表中的帖子:
<table class="table">
<caption><h3 style="text-align:right">h1</h3></caption>
<tr class="success">
<th style="text-align:center"> h2
<th style="text-align:center"> h3
<th style="text-align:center"> h4
</tr>
<?php
$cursor = $MySQLdb->prepare("SELECT * FROM comments WHERE post_id=:post_id");
$cursor->execute( array(":post_id"=>$post_id_to_show) );
foreach ($cursor->fetchAll() as $obj): ?>
<tr>
<td style="text-align:center"> <? echo $obj['comment_data']?></td>
<td style="text-align:center"><? echo $obj['full_name']?></td>
<td style="text-align:center"><? echo $obj['date_time']?></td>
</tr>
<? endforeach; ?>
</table>
我从网页收到此错误 注意:未定义变量:第 96 行 /opt/lampp/htdocs/big_project/post.php 中的 post_id_to_show
顺便说一句,这条线就是这条线
$cursor->execute( array(":post_id"=>$post_id_to_show) );
有什么问题吗?
【问题讨论】:
-
您是否首先尝试使用 Ajax 发布值,然后在将客户端重定向到同一页面后,尝试使用您在上一个请求中传递的变量?
-
@MagnusEriksson 不,我试图将请求发布到另一个页面 - post.php,但不知何故它没有得到它
-
“但不知何故它没有得到它” - 你是如何确定的?您只是向
post.php发布了一个值,但您对该请求的响应没有做任何事情。您所做的只是在带有 id 的 ajax 发布成功后,您使用 GET 将用户重定向到post.php并且没有任何数据。你知道你的$.post(...)代码会创建一个请求,而location.href = 'post.php';会创建一个完全不同的请求,没有第一个请求的任何参数,对吧? -
@MagnusEriksson 不,我不知道,我该怎么办?
标签: javascript php html sql