title: How to use AJAX in an MVC - php Blog? - adding comments dynamically
body: <p>I wrote a <code>posts.php</code> page which shows a certain post and its comments, giving the user the ability to <em>add a new comment</em> to this post dynamically.</p>
  
  <p>I want to implement the *"submit_comment"* using Ajax, but I don't really know how to do it in MVC.</p>
  
  <p>This is my Posts.php:</p>
  
  <pre><code>&lt;script type="application/javascript" src="Ajax/addComment.js"&gt; &lt;/script&gt; 
  src="../views/Ajax/addComment.js"&gt; &lt;/script&gt; &lt;title&gt; Posts
  (View)&lt;/title&gt; &lt;/head&gt;
  
  &lt;body&gt; &lt;div id="main"&gt; &lt;div class="container"&gt; &lt;?=$data['header'];?&gt;
  
  &lt;div id="content"&gt;  
      &lt;!-- Main Post --&gt;
      &lt;div class="content-background"&gt;
      &lt;h2&gt; &lt;?=$data['title'];?&gt;&lt;/h2&gt;
      &lt;h4&gt; &lt;?=$data['date'];?&gt; &lt;/h4&gt;
      &lt;p&gt; &lt;?=$data['content'];?&gt;&lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div id="form-content"&gt;
        &lt;form name="commentForm" method="post"&gt;
        Enter your name:  &lt;input type="text" name="username" id="username"&gt;&amp;nbsp; &lt;br /&gt;
        Enter your comment: &lt;/br&gt; &lt;textarea name="comment" id="comment" cols="10" rows="10"&gt; &lt;/textarea&gt; &lt;br /&gt;
  
        &lt;input value='Submit' type='button'  onclick='JavaScript:commentRequest2("&lt;?=$data['id']?&gt;")'
  name='submit'&gt;      
  
  
        &lt;/form&gt;
      &lt;/div&gt;
      &lt;div id="commentArea"&gt;
          &lt;?=include_once("comments_area.php");?&gt;
      &lt;/div&gt;&lt;!-- end commentArea --&gt;
  
  
      &lt;/div&gt;  &lt;/div&gt; &lt;/div&gt;
  
  
  &lt;/body&gt; &lt;/html&gt;
  </code></pre>
  
  <p>This is My Posts_Controller.php:</p>
  
  <pre><code>&lt;?php
  /**
   * This file handles the retrieval and serving of posts posts
   */
  class Posts_Controller
  {
      /**
       * This template variable will hold the 'view' portion of our MVC for this 
       * controller
       */
      public $template = 'posts';
  
      /**
       * This is the default function that will be called by router.php
       * 
       * @param array $getVars the GET variables posted to index.php
       */
      public function main(array $getVars)
      {
          $postsModel = new Posts_Model;
  
              }
              else{
          //get the post
          $post = $postsModel-&gt;get_main_post("`id`='", $getVars['id'], "LIMIT", "1");
          //get the comments
          $comments = $postsModel-&gt;get_comments("`postID`='", $getVars['id']);
          //create a new view and pass it our template
          $header = new View_Model('header_template');
          $view = new View_Model($this-&gt;template);
  
  
          //assign post data to view
          $view-&gt;assign('header', $header-&gt;render(FALSE));
          $view-&gt;assign('title' , $post['title']);
          $view-&gt;assign('content' , $post['content']);
          $view-&gt;assign('date' , $post['date']);
          $view-&gt;assign('by' , $post['added_by']);
          $view-&gt;assign('id' , $post['id']);
          $view-&gt;assign('commentsArr' , $comments);
          $view-&gt;render();
              }
      }
  }
  </code></pre>
  
  <p>And This is My Posts_Model.php:</p>
  
  <pre><code>&lt;?php
  /**
   * The Posts Model does the back-end heavy lifting for the Posts Controller
   */
  class Posts_Model
  {
      /**
       * Holds instance of database connection
       */
      private $db;
  
      public function __construct()
      {
          $this-&gt;db = new MysqlImproved_Driver;
      }
  
      /**
       * Fetches article based on supplied name
       * 
       * @param string $author
       * 
       * @return array $article
       */
      public function get_main_post($cond1, $var1, $cond2 ="", $var2 = "")
      {        
          //connect to database
          $this-&gt;db-&gt;connect();
  
          //sanitize data
          $var1 = $this-&gt;db-&gt;escape($var1);
          $var2 = $this-&gt;db-&gt;escape($var2);
          $cond = $cond1.$var1."'"; 
          $cond.= " ".$cond2." ".$var2;  
  
          //prepare query
          $this-&gt;db-&gt;prepare
          (
              "
              SELECT *  FROM `posts`
              WHERE $cond
              ;
              "
          );
  
          //execute query
          $this-&gt;db-&gt;query();
  
          $article = $this-&gt;db-&gt;fetch('array');
  
          return $article;
      }
  
  
      public function get_comments($cond1, $var1, $cond2 ="", $var2 = "")
      {        
          //connect to database
          $this-&gt;db-&gt;connect();
  
          //sanitize data
          $var1 = $this-&gt;db-&gt;escape($var1);
          $var2 = $this-&gt;db-&gt;escape($var2);
          $cond = $cond1.$var1."'"; 
          $cond.= " ".$cond2." ".$var2;  
  
          //prepare query
          $this-&gt;db-&gt;prepare
          (
              "
              SELECT *  FROM `comments`
              WHERE $cond
              ;
              "
          );
  
          //execute query
          $this-&gt;db-&gt;query();
          $resultArr[0] = 0;
          $i = 1;
          while( $result = $this-&gt;db-&gt;fetch('array')){
              $resultArr[$i] = $result;
              $i++;
          }
          $resultArr[0] = $i;
  
          return $resultArr;
      }
  
  }
  ?&gt;
  </code></pre>
  
  <p>Where Should I add the addComment.js? (V, M or C?)
  And what should the addComment do? what URL/ function to call?</p>
  
  <p>Any suggestions? Examples?</p>