title: Miracle in JS. One function with different result
body: <p>It's a Miracle! Help me to understand it.</p>
  
  <p>I have the some html code in my php file:</p>
  
  <pre><code>&lt;form action="" method="post" name="r_form" &gt;
  &lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt; Name: &lt;/td&gt;
  &lt;td&gt; &lt;input type="text" name="r_name" id = "rname" /&gt; &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
     &lt;td align="center" colspan="2"&gt; &lt;img src="FE.png" width="40" height="40" id="firstentryID" onClick="nextFunc()"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And the follow code in my auth.php:</p>
  
  <pre><code>    &lt;script type="text/javascript"&gt;
  
      Preload();
      nextFunc();
  
      function Preload() { 
      var valueToPush = {};
  var name2 = [];
      var userID1 = '&lt;?php echo $id_user;?&gt;';
                     $.ajax({
                         url: 'TR.php',
                          type: 'POST',
                          data: {userID1: userID1},
                         success: function(data){
                          var names = data
                  $('#result_div_id').html(data);
                               var json2 = $.parseJSON(data);
                       EntriesCount =json2.length; 
                 $.each(json2, function() {
                 $.each(this, function(k, v){
  
  
      switch(k) {
       case 'Name':
         valueToPush.Name = v;    
           break;
  
      // and more parametrs - it doesnt matter.
      }
      });
  
      name2.push({ID_Clients: valueToPush.id, Name: valueToPush.Name, Phone: valueToPush.Phone, Email: valueToPush.Email, Status: valueToPush.Status, Birthday: valueToPush.Birthday, ClientFrom: valueToPush.ClientFrom, User: valueToPush.User});
  
      });
      }
      });
      }
      ...
  </code></pre>
  
  <p>And in my nextFunc() I have:</p>
  
  <pre><code>function nextFunc(){
  
  document.getElementById("rname").value = name2[currententry]['Name'];
  }
  </code></pre>
  
  <p>In my JSON so I have:</p>
  
  <pre><code>[12]
  0:  {
  ID_Clients: "87"
  Name: "Вася"
  Phone: "1234578"
  Status: "4"
  Birthday: "2016-10-21"
  ClientFrom: "4"
  User: "2"
  }-
  1:  {
  ID_Clients: "88"
  Name: "Петя"
  Phone: "2345669"
  Status: "3"
  Birthday: "2016-10-11"
  ClientFrom: "2"
  User: "2"
  } ...  and so on...
  </code></pre>
  
  <p>The Miracle is:</p>
  
  <p>When I Call the NextFunc() from my  block like in this code i recieve the </p>
  
  <p><em>Uncaught TypeError: Cannot read property 'Name' of undefinedat NextFunc()</em></p>
  
  <p>But when I press the FE.png on my web page the code is working!!!</p>
  
  <p>WHY????</p>