title: Saving data in a JSON file with WordPress AJAX
body: <p>So I have a wordpress Ajax function that retrieves MySQL data as JSON and logs it. However I want to not directly get the data back on the page where my AJAX function is, but I want to save the data to a JSON file instead, so I can use it for a wider variety of purposes.</p>
  
  <p>Here is my AJAX function:</p>
  
  <pre><code>$.get(
          ajax_url,
          data,
          function(data) { // AJAX callback
              fill_json(data);
          }// End AJAX callback
      );
  </code></pre>
  
  <p>The <code>fill_json()</code> is a function to echo the JSON data in a table I wrote myself.</p>
  
  <p>Now here is what happens inside my AJAX hook:</p>
  
  <pre><code>$sql_search = $wpdb-&gt;get_results(" a complicated mysql search here ");
  if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
          $result = json_encode($result);         
          echo $result;
      } else {
          header("Location: ".$_SERVER["HTTP_REFERER"]);
      }
  </code></pre>
  
  <p>underneath <code>echo $result;</code> in my Ajax hook I tried the following piece of code, but I don't know how I can see if it worked or not:</p>
  
  <pre><code>        $json_path = "/var/www/vhosts/jtc.ae/httpdocs/pre/wp/wp-content/themes/Amazing_japan_HP/new/search.json";
          file_put_contents($json_path, $result);
  </code></pre>
  
  <p>My question:<br>
  <strong>Is this the correct way to save the data to a JSON file, and how can I get this data on my main page then?</strong></p>
  
  <p>Extra question: Will saving <code>$result</code> to a JSON file conflict with multiple users using the AJAX at the same time?</p>