title: ajax - sending data as json to php server and receiving response
body: <p>I'm trying to grasp more than I should at once. </p>
  
  <p>Let's say I have 2 inputs and a button, and on button click I want to create a json containing the data from those inputs and send it to the server.</p>
  
  <p>I think this should do it, but I might be wrong as I've seen a lot of different (poorly explained) methods of doing something similar.</p>
  
  <pre><code>    var Item = function(First, Second) {
          return {
              FirstPart : First.val(), 
              SecondPart : Second.val(), 
          };
      };
  
      $(document).ready(function(){
          $("#send_item").click(function() {
              var form = $("#add_item");
  
              if (form) {
                  item = Item($("#first"), $("#second"));
  
                  $.ajax ({
                      type: "POST",
                      url: "post.php", 
                      data: { 'test' : item },
                      success: function(result) {
                          console.log(result);
                      }
                  });
              }
          });
      });
  </code></pre>
  
  <p>In PHP I have</p>
  
  <pre><code>class ClientData
  {
      public $First;
      public $Second;
  
      public function __construct($F, $S)
      {
          $this-&gt;First = F;
          $this-&gt;Second = S;
      }
  }
  
  if (isset($_POST['test']))
  {
      // do stuff, get an object of type ClientData
  }
  </code></pre>
  
  <p>The problem is that $_POST['test'] appears to be an array (if I pass it to json_decode I get an error that says it is an array and if I iterate it using foreach I get the values that I expect to see).
  Is that ajax call correct? Is there something else I should do in the PHP bit? </p>