title: Calling JavaScript functions via PHP
body: <p>I have a PHP file which have variables collected from HTML. I have also a JavaScript function.</p>
  
  <p>I want to call the JavaScript function via PHP but I know it's impossible because the PHP code is before the JavaScript code.</p>
  
  <p>I put it before the JS code because JavaScript collects variables from the PHP code only when the PHP is before the JavaScript file.</p>
  
  <p>How can I call the JS function from PHP and still have the variables? (By echo 'after();'</p>
  
  <p>This is my index.php:</p>
  
  <pre><code>&lt;?php
  if(isset($_REQUEST['name']) &amp;&amp; isset($_REQUEST['secondName']))
  {
      $name = $_REQUEST['name'];
      $secondName = $_REQUEST['secondName'];
      echo '&lt;script&gt;after();&lt;/script&gt;';
  }
  else
  {
      $name = '';
      $secondName = '';
  }
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8" /&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function(){
      function after()
      {
      var name = '&lt;?=$name?&gt;';
      var secondName = '&lt;?=$secondName?&gt;';
      var str = "         ";
      if(!str.replace(/\s/g, '').length)
      {
          alert("error");
      }
      else
      {
          $("#pre-text").fadeIn();
          $("#text").html(fullName);
          $("#pre-text").click(function(){
          $(this).fadeOut();
          });
      }   
      }
  });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form method="get"&gt;
  &lt;p&gt;First name: &lt;input type="text" name="name"&gt;&lt;/p&gt;
  &lt;p&gt;Second name: &lt;input type="text" name="secondName"&gt; &lt;input type="submit"&gt;&lt;/p&gt;
  &lt;p id="pre-text" style="display:none"&gt;Your first name is: &lt;span id="text"&gt;&lt;/span&gt;. You can also click me to hide.&lt;/p&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks for help.</p>
  
  <p>EDIT: I want to call the function after().</p>