title: Use $.post methods returned function(data) value to variable
body: <p><strong>Dear Coders!</strong></p>
  
  <p>The purpose of my code:</p>
  
  <p>Get URL of files listed in specific folder, then assign them to an Array in javascript.</p>
  
  <p>How I'm imagining it:
  JavaScript function in <code>test.php</code> uses <code>$.post()</code> method to send a value to <code>getURL.php</code> file. After this, <code>getURL.php</code> uses this value to get specific file URLs in a specific folder. I'm getting the result(s) in the <code>$.post()</code> methods <code>function(data)</code> parameter. After this, the resulted value of the "data" is (/would be used) in JavaScript.</p>
  
  <p>The problem:
  Inside the <code>$.post()</code> methods function: <code>function(data,status) I'm satisfied with the result of the returned value of the data parameter; the PROBLEM is that I can't assign it's value outside this function:</code>function (data,status)`.</p>
  
  <p>TEST.php</p>
  
  <pre><code>&lt;script src="jquery-1.9.1.js"&gt;
  &lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      var imgPath; // &lt;--He is who should get the value of "data"
      function getTargetUrl(szolg){
          $.post(
              "getURL.php",
              { x: szolg },
              function(data,status){
                  alert("in function: " + data + " status: " + status);
                  imgPath=data;
                  alert (imgPath);
              }
          );
      }
      $(document).ready(function() {
          var a="szolg5"; //it will be "user defined", used in getURL.php
          getTargetUrl(a);
          alert(imgPath);
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>getURL.php</p>
  
  <pre><code>&lt;?php 
  if(isset($_POST["x"])){
      $queryGlob='img/'.$_POST["x"].'/batch/*.jpg';
      foreach (glob($queryGlob) as $filename) {
          $imgFiles=json_encode($filename);
          $imgFiles=str_replace('\/','/',$imgFiles);
          echo $imgFiles;
      }
      //$data = str_replace('\\/', '/', json_encode(glob('img/'.$_POST["x"].'/batch/*.jpg')));
  }
  else{
      $imgFiles="FAIL";
      echo $imgFiles;
  }
  ?&gt;
  </code></pre>
  
  <p>Note: for testing I'm using Google Chrome.</p>
  
  <p>So that's all I guess, hope someone can give me a solution and possible explanation.</p>