简体   繁体   English

在Angularjs和PHP中使用id向特定帖子添加评论

[英]Adding comment to specific post using id with Angularjs and PHP

I am new to Angular and am struggling to add a comment to a specific post that is being displayed from a database. 我是Angular的新手,正在努力为从数据库中显示的特定帖子添加评论。 Under the post I have a hidden comment area that shows when pressing the button. 在帖子下方,我有一个隐藏的评论区域,当按下按钮时会显示该区域。 I then want to be able to add the comment to the post the comment area is attached too. 然后,我希望能够将评论添加到附加评论区域的帖子中。 It seems like it is not getting the id but I can't find how to solve it. 似乎没有获取ID,但我找不到解决方法。 It is doing the show/hide, and I am getting the "data inserted" in the console log so I'm guessing the problem is with the PHP? 它正在执行显示/隐藏操作,并且我在控制台日志中获取了“插入的数据”,因此我猜测问题出在PHP?

Angular code 角度代码

app.controller('meetings', function($scope, $http) {
$scope.meetings_insertdata = function() {
$http.post ("meetings_insert.php",{'meetings_commentdate':$scope.meetings_commentdate,'meetings_comment':$scope.meetings_comment})
window.location.reload();
console.log("data inserted");
    };
});



app.controller('show_hide', function ($scope) {
            $scope.Hidden = true;
            $scope.ShowHide = function () {
                $scope.Hidden = $scope.Hidden ? false : true;
            }

   });

meetings_insert.php Meetings_insert.php

<?php

$data = json_decode(file_get_contents("php://input"));
$meetings_comment = $data->meetings_comment;
$meetings_commentdate = date('Y-m-d H:i:s');
$meetings_entry = $_GET['id'];

mysql_connect("localhost","user","password");
mysql_select_db("mdb_rh316");



mysql_query("UPDATE project_meetings SET (meetings_comment, meetings_commentdate) = ('$meetings_comment','$meetings_commentdate') WHERE meetings_entry = '$meetings_entry'");

?>

HTML code HTML代码

<div class="entrybox" ng-controller="meetings">
<?php 
mysql_connect("localhost","user","password");
mysql_select_db("mdb_rh316");

$result = mysql_query("SELECT * FROM project_meetings ORDER BY meetings_date DESC");
while ($row = mysql_fetch_array($result))
{
echo "<table>";
        echo "<tr><td>" ?>Added: <? echo $row['meetings_date'] . $row['meetings_entry'] . "<br/>" . $row['meetings_content']."</td></tr>";
        echo "<tr><td>" ?><br/><span class="emp">Comment: <?
        echo $row['meetings_commentdate']."<br/>" . $row['meetings_comment']."</td></tr>";
        echo "<tr><td>"?></span>
        <div ng-controller="show_hide">
        <input type="button" class="previous_add" value="Add comment" ng-click="ShowHide()" />
        <br />
        <br />
        <div ng-hide = "Hidden">
<form method="post" action="meetings_insert.php?id=<? echo $row['$meetings_entry']?>"> 
<textarea class="textarea" placeholder="Add your comment here.." type="text" ng-model="meetings_comment" name="meetings_comment"></textarea><br/>
<input type="button" class="button" value= "Add" ng-click="meetings_insertdata()"/>
</form>
        </div>
    </div>
</td></tr><br/><?;

}

echo "</table>";
?>
</div>

Pass 'id' into the meetings_insertdata function: 将'id'传递给meeting_insertdata函数:

 <form method="post" action=""> <textarea class="textarea" placeholder="Add your comment here.." type="text" ng-model="meetings_comment" name="meetings_comment"></textarea> <br/> <input type="button" class="button" value="Add" ng-click="meetings_insertdata(<? echo $row['$meetings_entry']?>)" /> </form> 

Receive it in the AngularJS function below: 在下面的AngularJS函数中接收它:

 app.controller('meetings', function($scope, $http) { $scope.meetings_insertdata = function(id) { $http.post("meetings_insert.php", { 'meetings_commentdate': $scope.meetings_commentdate, 'meetings_comment': $scope.meetings_comment, 'meetings_event': id }) window.location.reload(); console.log("data inserted"); }; }); 

Then, pick up 'id' from the posted value ($data->meetings_event) 然后,从发布的值中选择“ id”($ data-> meetings_event)

$data = json_decode(file_get_contents("php://input")); $ data = json_decode(file_get_contents(“ php:// input”));
$meetings_comment = $data->meetings_comment; $ meetings_comment = $ data-> meetings_comment;
$meetings_commentdate = date('Ymd H:i:s'); $ meetings_commentdate = date('Ymd H:i:s');
$meetings_entry = $data->meetings_event; $ meetings_entry = $ data-> meetings_event;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM