【问题标题】:getting error while doing post request to another php page向另一个 php 页面发送请求时出错
【发布时间】: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


【解决方案1】:

首先我像这样通过 ajax 传递信息。

$(".click_title").click(function(){
        var $item = $(this).closest("tr").find(".post_id_c").text();//עובד בטוח
        
                if($item){
                    $.ajax({
                url: 'post.php',
                type: 'POST',
                data: {post_id_to_show:$item},
                success: function(data) {
                    location.href = "post.php";
                    console.log(data); // Inspect this in your console
                }
            });
        };
    });

在第一次调用 post_id_to_show 时,我将其切换到会话变量。 在第二次通话中,我使用了 session var,它起作用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2012-07-28
    • 2012-12-21
    • 1970-01-01
    • 2013-05-27
    • 2020-07-01
    • 1970-01-01
    相关资源
    最近更新 更多