title: Submit Form PHP amend to JQuery AJAX
body: <p>I'm looking to convert the below php form submission to a JQuery Ajax submission. I have used Ajax with some simple requests before, but I'm not sure how to submit and return data from MySQL for the below code.   </p>
  
  <p>The code below submits the user input entry to a MySql query returning single columns rows. A While loop then looks at these rows and fires another mysql query returning the number of user likes per row.</p>
  
  <pre><code>&lt;?php
  
  if(!empty($_POST['Message']))
  {
        $userid = session_id();
        $searchStr = get_post($con,'Message');
        $aKeyword = explode(" ", $searchStr);
  
        $aKeyword = array_filter($aKeyword);
  
        $stmt = $con-&gt;prepare(
            'SELECT m.ID, m.MessageText 
            FROM MessageMain m 
            LEFT OUTER JOIN Likes l on m.ID = l.PostID 
            WHERE MessageText REGEXP ?
            GROUP BY m.ID, m.MessageText ORDER BY count(m.id) desc'
        );
  
        $regexString = implode('|', $aKeyword);
        $stmt-&gt;bind_param('s', $regexString);
        $stmt-&gt;execute();
        $result = $stmt-&gt;get_result();
  
         $rowcount=mysqli_num_rows($result);
  
         echo "&lt;pre&gt; Returned ".  $rowcount . " matches&lt;/pre&gt;";
  
       if(mysqli_num_rows($result) &gt; 0) {
          $row_count=0;
          While($row = $result-&gt;fetch_assoc()) {   
              $postid = $row['ID'];
              $row_count++;
  
                      // Checking user status
                      $status_query = $con-&gt;prepare("SELECT COUNT(*) AS type FROM likes WHERE userid = ? AND postid = ?");
                      $status_query-&gt;bind_param('ss',$userid,$postid);
                      $status_query-&gt;execute();
                      $status_result = $status_query-&gt;get_result();
                      $status_row = $status_result-&gt;fetch_assoc();
                      $type = $status_row['type'];
  
                      // Count post total likes
                      $like_query = $con-&gt;prepare("SELECT COUNT(*) AS cntLikes FROM likes WHERE postid = ?");
                      $like_query-&gt;bind_param('s',$postid);
                      $like_query-&gt;execute();
                      $like_result = $like_query-&gt;get_result();
                      $like_row = $like_result-&gt;fetch_assoc();
                      $total_likes = $like_row['cntLikes'];
  
  ?&gt;
                      &lt;div class="post"&gt;
                          &lt;div class="post-text"&gt;
              &lt;?php
  
              echo nl2br(htmlentities($row['MessageText'],ENT_COMPAT|ENT_IGNORE, "UTF-8") );
  
              ?&gt;
  
               &lt;/div&gt;
                          &lt;div class="post-action"&gt;
  
                              &lt;input type="button" value="Like" id="like_&lt;?php echo htmlentities($postid . "_" . $userid); ?&gt;" class="like" style="&lt;?php if($type == 1){ echo "color: #ffa449;"; } ?&gt;" /&gt;&amp;nbsp;(&lt;span id="likes_&lt;?php echo $postid . "_" . $userid; ?&gt;"&gt;&lt;?php echo htmlentities($total_likes); ?&gt;&lt;/span&gt;)&amp;nbsp;
  
                          &lt;/div&gt;
                      &lt;/div&gt;
          &lt;?php 
                  }
  
      }
  
  }
  </code></pre>