title: Passing a PHP variable with fetched data to a JavaScript variable returns NULL or empty
body: <p>I have an issue with returning the value of a PHP variable in JS. It returns <code>NULL</code> or empty instead of returning the age.</p>
  
  <p><strong>Approach:</strong></p>
  
  <p>Passing PHP variable with data to a JS variable in a separate file. Display JS variable in an <code>alert()</code>. Data was fetched from the database using <code>fetch_assoc()</code> in a while loop. Without using Ajax!</p>
  
  <p><strong>Proposed plan:</strong></p>
  
  <ol>
  <li><p>Enter a name.</p></li>
  <li><p>Submit.</p></li>
  <li><p>PHP fetches the age associated with that name.</p></li>
  <li><p>age is stored in a PHP variable <code>dbage</code>.</p></li>
  <li><p>Passed into JS variable to alert user what their age is.</p></li>
  </ol>
  
  <p>I am trying to pass <code>$dbage</code> from <strong>sampletest.php</strong> to <code>user</code> in <strong>sample.php</strong> which will onsubmit display an alert saying: "Your age is blah".</p>
  
  <p><code>blah</code> is <code>$dbage</code>, which contains the age. This is for testing. Once I understand why this isn't working, I can move on to sending these JS variables to functions that will do calculations and return back to the DB.</p>
  
  <p><strong>What I have tried so far..</strong></p>
  
  <ol>
  <li><p>Trying to catch <code>echo</code> using <code>ob_start()</code> but that returned <code>NULL</code> as well.</p>
  
  <p>Example: </p>
  
  <pre><code>   ob_start();
     echo $dbage;
     $output = ob_get_contents();
     ob_end_clean();
  </code></pre></li>
  <li><p>Making <code>$dbage</code> a global variable. Returns empty.</p></li>
  <li><p>Echo variable outside the <code>while</code> loop but that returned <code>NULL</code>. </p>
  
  <p>Example:</p>
  
  <pre><code>    $dbage = '';
      while( $row = $result-&gt;fetch_assoc()) {
          $dbage = $row['age'];
      }
      echo $dbage;
  </code></pre></li>
  </ol>
  
  <p>Any suggestions, corrections are appreciated.</p>
  
  <p><strong>sample.php (index file)</strong></p>
  
  <pre><code>&lt;?php
  include 'sampletest.php';
  session_start();    
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;    
  &lt;body&gt;
       &lt;div id="id03"&gt;
        &lt;form class="modal-content" action="sampletest.php" method="post" onsubmit="myFunction()"&gt;
          &lt;div class="container"&gt;
            &lt;input type="text" id="name" placeholder="Enter name"  name="name"&gt;
              &lt;div class="clearfix"&gt;
              &lt;button type="submit" class="loggedinbtn" name="load"/&gt;Load
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/form&gt;
      &lt;/div&gt;    
  
      &lt;script&gt;
      function myFunction() {
          var user = '&lt;?php echo(json_encode($dbage)); ?&gt;';
          alert("This is a php varible " + user);
      }
      &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>sampletest.php</strong></p>
  
  <p>
  
  <pre><code>if(isset($_POST['load'])){
      require 'config.php';
      $name = $_POST['name'];
      $age = $_POST['age'];
      if(empty($name)) {
          echo "Enter a number";
      }elseif(!preg_match('/^[a-z ]+$/i', $name)){
          echo "Enter a letter, no numbers";
      }else{
          if ($conn-&gt;connect_error) {
              die("Connection failed: " . $conn-&gt;connect_error);
              header("location: sample.php?Connect-database=failed");
              exit();
          } 
          $sql = "SELECT name, age FROM results WHERE name= '$name';";
          $result = $conn-&gt;query($sql);
  
          if ($result-&gt;num_rows &gt; 0) {
              while( $row = $result-&gt;fetch_assoc()) {                    
                   $dbage = $row['age'];
              }
          }
          else{
              echo "0 results";
          }
          $conn-&gt;close();
      }    
  }
  </code></pre>