title: Send filepath to PHP using AJAX and jQuery, and get back file contents
body: <p>I am trying to:<br>
  -Send a filepath to a server with post<br>
  -Have PHP on the server receive the post, and get the file contents<br>
  -Have the server respond with the file contents<br>
  <em>I am aware that this poses some security risks. There will eventually be a system in place to prevent access to certain files depending on who is logged in. Right now im just trying to get the system to work</em><br>
  <strong>The JS:</strong><br></p>
  
  <pre><code>function getFileContentsP(path, contents) {
      $.post("/os/php/file_get_contents.php",
          {"path": path},
          function (data, status, jqXHR) {
                  contents = data;
              },
          );
      return contents;
  }
  </code></pre>
  
  <p><strong>The PHP:</strong><br></p>
  
  <pre><code>&lt;?php
      if ($_SERVER["REQUEST_METHOD"] == "POST") {
          var $filePath = testInput($_POST["path"]);
          // returns file contents
          echo file_get_contents($filePath);
      }
  
      function testInput($input) {
          $input = trim($input);
          //$input = stripslashes($input); filepaths have slashes
          $input = htmlspecialchars($input);
          return $input;
      }
  ?&gt;
  </code></pre>
  
  <p>There is a file in the same directory as the php file.
  Using the JS function from the console to get this file returns undefined.
  This works so horribly that the file path does not even seem to reach the PHP (Discovered by using a log file that the php could write to).<br>
  Am I missing something?</p>