title: Processing json serialized form data in php
body: <p>I need to store the variable from the json string with name 'msg' in my database, but I am not able to catch it with  $msg = $_POST['msg']; How do I catch it correctly?</p>
  
  <p>Moreover I would like to echo the contents of $msg straightaway on the webpage.</p>
  
  <h2>HTML</h2>
  
  <pre><code>&lt;div id="addCommentContainer"&gt;
      &lt;form id="addCommentForm" action=""&gt;
          &lt;textarea name="msg" id="msg" cols="82" title="Your comment" rows="2"&gt;Your comment...&lt;/textarea&gt;
          &lt;br /&gt;
          &lt;input type="text" name="author" title="name" value="&lt;?php echo $_SESSION['username']; ?&gt;" id="author" /&gt;
          &lt;br /&gt;
          &lt;div id="personal"&gt;
              &lt;input type="text" name="city" id="city" title="city (optional)" value="" /&gt;
              &lt;br /&gt;
              &lt;input type="text" name="email" id="email" title="e-mail (optional)" value="" /&gt;
              &lt;br /&gt;
              &lt;input type="text" name="url" id="url" title="website (optional)" value="" /&gt;
              &lt;input type="hidden" id="cam_id" class="hd" name="cam_id" value="&lt;?php echo $cam_id ?&gt;" /&gt;
          &lt;/div&gt;
          &lt;input type="submit" id="submit" value="Comment" /&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <h2>JavaScript</h2>
  
  <pre><code>//if submit button is clicked
  $('#submit').click(function () {
      //start the ajax
      $.ajax({
          //this is the php file that processes the data 
          url: "/comment/insert.php",
          type: "POST",
          data: $("#addCommentForm").serialize(),
          contentType: "json",
          //success
          success: function (html) {
              //if returned 1/true (process success)
              if (html == 1) {
                  //show the success message
                  $('.done').fadeIn('slow');
                  //if process.php returned 0/false (send mail failed)
              } else alert('Sorry, unexpected error. Please try again later.');
          }
      });
      //cancel the submit button default behaviours
      return false;
  });
  </code></pre>
  
  <h2>PHP</h2>
  
  <pre><code> $msg = $_POST['msg'];
  //  echo $msg;
   $author = $_POST['author'];
   $email = $_POST['email'];
   $url = $_POST['url'];
   $city = $_POST['city'];
  
  
  
  
      // include ("/home/sionvalais/domains/skiweather.eu/public_html/v3/functions/strip.php");
  
      if ($cam_id&gt;1) {
  
      if ($author=='NAME') {
      $author='Anonymous';
      }
  
      $host = gethostbyaddr($ip);
      // mysql_query ("set character_set_results='utf8'");
      mysql_query("INSERT INTO sv_review (author,email,msg,cam_id,url,lud)
                      VALUES (
                          N'".$author."',
                          '".$email."',
                          N'".$msg."',
                          '".$cam_id."',
                          '".$url."',
                          NOW()
                      )");
       }
  </code></pre>