第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 AJAX 時從頁面獲取數(shù)據(jù)消失

使用 AJAX 時從頁面獲取數(shù)據(jù)消失

PHP
有只小跳蛙 2022-07-02 16:29:03
一種使用 PHP/Ajax/SQL 的新手。我目前在一個小項目中工作,我們被要求使用上述語言創(chuàng)建一個 Facebook 風格的網(wǎng)站。我正在嘗試創(chuàng)建頁面,用戶可以在其中查看各個帖子和每個帖子中的評論。用戶還可以在同一頁面上發(fā)表評論。我還有一個數(shù)據(jù)庫來存儲項目所需的不同詳細信息(帖子、用戶詳細信息、評論等)從顯示帖子的頁面獲取的數(shù)據(jù)通過 GET 方法傳遞。還有一個 ajax 函數(shù)用于將評論數(shù)據(jù)插入我的數(shù)據(jù)庫。問題是,每次我嘗試將數(shù)據(jù)插入數(shù)據(jù)庫(通過 AJAX)時,都會出現(xiàn)以下錯誤:Notice: Undefined index: blog_title in C:\xampp\htdocs\postpage.php on line 24我還注意到,當我嘗試調用 AJAX 函數(shù)時,通過 GET 方法傳遞的數(shù)據(jù)消失了。我能做些什么來解決這個錯誤?通過此處獲取發(fā)布數(shù)據(jù)(第 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>先感謝您!
查看完整描述

2 回答

?
揚帆大魚

TA貢獻1799條經(jīng)驗 獲得超9個贊

在您的 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 請求。


       $.ajax({

            method:"GET",   //changed method to GET

            url:"comment.php",

            data:{

                "comment":comment,

                "commenter_name":username,

                "commented_post":title,

            },

            dataType = "json",

            success: function(result){

                alert("Comment posted.");

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

            }

        });


查看完整回答
反對 回復 2022-07-02
?
慕碼人8056858

TA貢獻1803條經(jīng)驗 獲得超6個贊

像 Lal 回答一樣,首先需要轉換為 GET 方法,然后需要聲明 blog_title 參數(shù)。


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

        <script>

            function Comment(comment, username, title){

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

                $.ajax({

                    method:"GET", // Changed method to GET

                    url:"comment.php",

                    data:{

                        "comment":comment,

                        "commenter_name":username,

                        "commented_post":title,

                        "blog_title":title  //You must declare the blog_title parameter here

                    },

                    dataType = "json",

                    success: function(result){

                        alert("Comment posted.");

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

                    }

                });

            }


        </script>


查看完整回答
反對 回復 2022-07-02
  • 2 回答
  • 0 關注
  • 195 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號