title: How to get JSON sent by AJAX to work with PHP
body: <p>I am hitting roadblock after roadblock in my quest to get this JSON usable by PHP, so I'm wondering if someone can help me out here.  </p>
  
  <p>I have JSON being stored in the variable divisionsJSON:  </p>
  
  <pre><code>    var divisionsJSON = JSON.stringify(divisions);  
  </code></pre>
  
  <p><br>
  Then I try to use .ajax to post with the following:<br>
  <br></p>
  
  <pre><code>$.ajax({
       url: "divisions.php",
       type: "post",
       data: divisionsJSON,
       success: function(){
       alert("success");
         $("#result").html('submitted successfully');
       },
       error:function(){
        alert("failure");
        $("#result").html('there is error while submit');
       }   
       }); 
  </code></pre>
  
  <p>(I copied this from another question on SO, but have nothing in my html with the id="result" - I think I can delete that part [deletion confirmed])  </p>
  
  <p>Then, my divisions.php page contains the following:  </p>
  
  <pre><code>&lt;?php 
      $url = "divisions.php";
      $json = file_get_contents($url);
      $json = utf8_encode($json);
      $elements = json_decode($json);
      var_dump($elements);
    ?&gt;
  </code></pre>
  
  <p>My inexperience with PHP/ajax combined with my frustration with getting this to work has made me try a bunch of different things. So far I've either gotten NULL or nothing when I load divisions.php. My gut tells me that it's a problem with the ajax, but I'm so inexperienced with PHP that I can't say with confidence that my PHP is correct enough to where I <i>should</i> be getting something back. I've tried var_dump, print_r, echo, absolutely nothing is showing up on divisions.php related to the actual PHP/JSON. Any and all help would be greatly appreciated!</p>
  
  <p>Response with updated code:  </p>
  
  <p>I am getting NULL with the following php without the utf8 line, and string(0) "" when I added in the utf8 line:  </p>
  
  <pre><code>&lt;?php
          $json = json_decode(file_get_contents('php://input'));
          $elements = utf8_encode($json);
          var_dump($elements);
  ?&gt;
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Edited for full php page:  </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;html lang="en"&gt;
  
  &lt;head&gt;  
      &lt;meta charset="utf-8"&gt;
      &lt;title&gt;untitled&lt;/title&gt;
      &lt;link rel="stylesheet" href="styles/reset.css" /&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="underscore.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="application.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="divisions.js"&gt;&lt;/script&gt;
  &lt;?php
          print_r($_POST);
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I've tried multiple lines for the php, print_r($_POST);, json_decode($_POST);, etc. but I'm changing so many things based on what I'm seeing on other SO's and this page that I'm really in a daze. </p>
  
  <p>EDIT: Would this code, which I intended to make an array, not produce JSON? Because it doesn't seem like I'm working with JSON atm.  </p>
  
  <pre><code>var divisions = [];
  for (var i = 0; i &lt; arr.length; i++) {
      if(arr[i].value != "N/A"){
      var obj = { "login": arr[i].login,
              "value": "http://www.value.net/index/" + arr[i].value};
      divisions.push(obj);}
  </code></pre>