使用 AJAX 时从页面获取数据消失

一种使用 PHP/Ajax/SQL 的新手。


我目前在一个小项目中工作,我们被要求使用上述语言创建一个 Facebook 风格的网站。我正在尝试创建页面,用户可以在其中查看各个帖子和每个帖子中的评论。用户还可以在同一页面上发表评论。我还有一个数据库来存储项目所需的不同详细信息(帖子、用户详细信息、评论等)


从显示帖子的页面获取的数据通过 GET 方法传递。还有一个 ajax 函数用于将评论数据插入我的数据库。问题是,每次我尝试将数据插入数据库(通过 AJAX)时,都会出现以下错误:


Notice: Undefined index: blog_title in C:\xampp\htdocs\postpage.php on line 24

我还注意到,当我尝试调用 AJAX 函数时,通过 GET 方法传递的数据消失了。我能做些什么来解决这个错误?


通过此处获取发布数据(第 24 行):


    $blog_title = $_GET['blog_title'];

HTML:


    <!DOCTYPE html>

<html>

    <head>

        <title><?=$blog_title?></title>

    </head>


    <body>

        <div class = "details">

            <h2><?=$row['title']?></h2>

            <h3>Published by: <a link href = "<?=$location?>"><?=$row['author_name']?></a></h3>

            <h4>Date Posted: <?=$row['date']?> at <?=$row['time']?></h4>

        </div>

        <br>

        <p>

            <?=$row['content']?>

        </p><br><br>

        <div class = "commentarea">

            <form>

                <label>Commenting as: <?=$my_username?></label><br>

                <textarea rows = "10" cols = "20" id = "comment" required></textarea>

                <button name = "comment" id = "post" onclick="Comment($('#comment').val(), <?=$my_username?>, <?=$blog_title?>)">Submit Comment</button>

            </form>

        <div>

AJAX 功能:


    <script src = "js/jquery.js"></script> 

    <script>

        function Comment(comment, username, title){

        //alert(search + " " + filter);

            $.ajax({

                method:"POST",

                url:"comment.php",

                data:{

                    "comment":comment,

                    "commenter_name":username,

                    "commented_post":title,

                },

                dataType = "json",

                success: function(result){

                    alert("Comment posted.");

                    $("#record").html(result);

                }

            });

        }


    </script>

先感谢您!


有只小跳蛙
浏览 170回答 2
2回答

扬帆大鱼

在您的 Ajax 调用中,您将方法指定为POST. 但是在您的 PHP 页面中,您正在尝试使用请求来获取请求$_GET['blog_title'],这就是您收到未定义变量错误的原因。而不是$_GET你应该使用$_POST或$_REQUEST。也就是说,更换$blog_title = $_GET['blog_title'];和$blog_title = $_POST['blog_title'];或者$blog_title = $_REQUEST['blog_title'];在文档中阅读有关$_REQUEST和$_POST的更多信息。或者您可以如下更改您的 ajax 请求。&nbsp; &nbsp; &nbsp; &nbsp;$.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method:"GET",&nbsp; &nbsp;//changed method to GET&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url:"comment.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "comment":comment,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "commenter_name":username,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "commented_post":title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType = "json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(result){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Comment posted.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#record").html(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });

慕码人8056858

像 Lal 回答一样,首先需要转换为 GET 方法,然后需要声明 blog_title 参数。<script src = "js/jquery.js"></script>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function Comment(comment, username, title){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //alert(search + " " + filter);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method:"GET", // Changed method to GET&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url:"comment.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "comment":comment,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "commenter_name":username,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "commented_post":title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "blog_title":title&nbsp; //You must declare the blog_title parameter here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType = "json",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function(result){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Comment posted.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#record").html(result);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP