title: AJAX example with JSON Response with object being passed
body: <p>I'm having a difficult time using AJAX to send an associative array to a php file. There's somethings I'm not understanding clearly. Here's my code to make the array from a form of input tags, but I don't know how to send it and interpret it in php.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function(){
    $(':submit').on('click', function() { // This event fires when a button is clicked
        var theData = {};
        $(":input:not(:button)").each(
          function(index){  
              var input = $(this);
              theData[input.attr('name')] = input.val();
          }
        );
        $.ajax({ // ajax call starts
            url: "http://www.aberlechiropractic.com/meningealrelease/modifydoctors/modifydoctors3.php",
            data: theData,
            dataType: 'json',
            success: function(data)
            {
                $('#wines').html(''); // Clear #wines div
                $('#wines').append('Data Received: ' + data.name+'   '+data.address + '&lt;br/&gt;');
            }
        });
        return false; // keeps the page from not refreshing 
    });
  });
  &lt;/script&gt;
  
  &lt;body&gt;
    &lt;form&gt;
      &lt;input type="text" name="name" id="name" value="Jeff Aberle"/&gt;
      &lt;input type="text" name="address1" id="address1" value="4710 East Broadway"/&gt;
      &lt;button type="submit" name="updatedoctor" id="updatedoctor" value="all"&gt;All&lt;/button&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Here's my php code:</p>
  
  <pre><code>&lt;?php
  $name = $_GET['name'];
  $address1 = $_GET['address1'];
  $array = array($button, $address1);
  print json_encode($array);
  ?&gt;
  </code></pre>
  
  <p>Ah now everything works. I edited all the code here to make this work.</p>
  
  <pre><code>&lt;?php
  // Get value of clicked button
  $name = $_GET['name'];
  $address1 = $_GET['address1'];
  $array = array(
      "name"    =&gt; $name,
      "address"  =&gt; $address1,
  );
  print json_encode($array);
  ?&gt;
  </code></pre>
  
  <p>I also have a div with id=wines . It was another thing I forgot to show. That's where the data is being returned to and displayed without the name however.</p>