想要在不提交表单的情况下在文本区域中输入注释

时间:2013-09-28 11:28:10

标签: php html sql

我想收集特定用户为图片提供的评论。这一切都是通过点击评论链接完成的。点击链接后我必须带有评论和图片名称。 这是评论页面的代码,它全部在表单

echo"<tr><td colspan='2'>Your Comment: </td>";
echo" <td colspan='5'><textarea name='comment' id='comment' rows='2' cols='25'></textarea></td></tr>";
echo"<tr><td colspan='2'><a href=\"comment.php?id1=".$file."\">Comment</a></td></tr>";

在评论.php中,我无法得到评论。 这是我的评论.php,

<?php
 session_start();
  $id=$_GET['id'];
$dsn = 'mysql:host=127.0.0.1;dbname=as1';
 $user = 'root';
 $password = '';
//$u=$_POST['comment'];
try {
  // Connect and create the PDO object
 $pdo = new PDO($dsn, $user, $password);
 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
 echo 'Database connection failed - ';
 echo $e->getMessage();
 exit;
 }
echo $u."posted in".$id;
 //$sql = "INSERT  comment_table (imageId) SELECT (id) FROM photo WHERE imagename=?";
   // $q = $pdo->prepare($sql);
    //$q->execute(array($_SESSION['comment']));
    //echo $_SESSION['uname'];      

 ?>

我无法在此处收到评论

1 个答案:

答案 0 :(得分:1)

<div class="commentBox">
    <img src="http://icons.iconarchive.com/icons/custom-icon-design/valentine/256/heart-icon.png"/>

    <div class="commentList">
        <div class="comment" data-bind="foreach:comments">
             <h2 class="commentAuthor" data-bind="text:author">
       </h2>
 <span data-bind="html:text">
       </span>

        </div>
        <form name="commentForm" class="commentForm" data-bind="with:newComment">
            <input type="text" data-bind="value:author" required/>
            <input type="text" data-bind="value:text" required />
            <button type="button" data-bind="click:$parent.addComment">Add</button>
        </form>
    </div>
</div>

脚本文件:

 ko.validation.rules.required.message = 'Required Field.';

 ko.validation.configure({
     registerExtenders: true,
     messagesOnModified: true,
     insertMessages: true,
     parseInputAttributes: true,
     messageTemplate: null
 });

 var shayJS = (function ($, ko) {

     //A simple promise returning DAL 
     var DAL = {};
     DAL.insert = function (api, payload) {
         return $.ajax({
             url: '/echo/json/',
             type: 'POST',
             dataType: 'json',
             data: {
                 json: "{}"
             }
         });
     };

     DAL.get = function (api, payload) {
         return $.ajax({
             url: '/echo/json/',
             type: 'POST',
             dataType: 'json',
             data: {
                 json: JSON.stringify([{
                     author: '',
                     text: ''
                 }, ])
             }
         });
     };


     //Knockout Model
     function CommentViewModel() {
         var self = this;

         //Comment Class
         function Comment() {
             var self = this;
             self.author = ko.observable().extend({
                 required: true
             });
             self.text = ko.observable().extend({
                 required: true
             });
             ko.validation.group(self);
         }

         self.newComment = ko.observable(new Comment());
         self.comments = ko.observableArray([]);

         self.addComment = function () {
             if (self.newComment().errors().length === 0) {
                 //Save the comment to the server
                 DAL.insert("comment", ko.toJSON(self.newComment()))
                     .done(self.afterSave);

             } else {
                 self.newComment().errors.showAllMessages();

             }

         };

         self.afterSave = function () {
             //Add to the List
             self.comments.push(self.newComment());
             //Re Initialise the new Comment
             self.newComment(new Comment());
         };

         //get comments from server
         self.getLatestComments = function () {
             return DAL.get("comment", {});
         };

         //Initialize the comment list, call server and update comments array
         self.getLatestComments().done(self.comments);

     }

     ko.applyBindings(new CommentViewModel());

 }($, ko));

demo