title: writing a json object to .json file using javascript and php
body: <p>I need to write a json object to a .json file using javascript and php. The json object seems to be storing the values correctly, but the php file doesn't seem to run so nothing happens.</p>
  
  <p>Thanks.</p>
  
  <p>my javascript function, which is called on a button click:</p>
  
  <pre><code>    function save_crime()
  {
      //json object, v1, v2, v3 etc are variables whose values are set in a different function
      var jsonObject = { "crime" : {  "violence" : {  "violence1" : v1,
                                                      "violence2" : v2,
                                                      "violence3" : v3
                                      },
  
                                      "burglary" : {  "burglary1" : b1,
                                                      "burglary2" : b2
                                      },
  
                                      "robbery" :  {  "robbery1" : r1,
                                                      "robbery2" : r2
                                      },
  
                                      "criminal" : {  "criminal1" : c1,
                                                      "criminal2" : c2,
                                                  "criminal3" : c3
                                      } 
                                  } 
                      }
  
  
  
      //jQuery to post json object to json.php for writing to json file
      $.ajax({
          type : "POST",
          url : 'json.php',
          dataType : 'json', 
          data : {
              json : JSON.stringify(jsonObject)
          }
      });
  
  }
  </code></pre>
  
  <p>php file for writing to .json file:</p>
  
  <pre><code>&lt;?php
  
  $json = $_POST['json'];
  $info = json_encode($json);
  
  $file = "crimes.json";
  $handle = fopen($file, 'w');
  fwrite($handle, $info);
  fclose($handle);
  
  ?&gt;
  </code></pre>