title: Echoing php inside javascript
body: <p>Trying to display the message in a MySQL table to all users' screens using xmlhttp, toastr alert and php.</p>
  
  <p>I use <code>xmlhttp.open("GET","includes/ajax/user/general/messages.php?username=&lt;?php echo $_SESSION['username']; ?&gt;",true);</code> which is where the SQL is and I put this on a 5 second loop so it checks <code>messages.php</code> every 5 seconds.</p>
  
  <p>After I then do:</p>
  
  <pre><code>xmlhttp.send();
  
  $.toast({
  heading: &lt;?php echo json_encode($messageInfo['title']); ?&gt;,
  text: &lt;?php echo json_encode($messageInfo['message']); ?&gt;,
  position: 'top-right',
  loaderBg: '#ff6849',
  icon: &lt;?php echo json_encode($messageInfo['type']); ?&gt;,
  hideAfter: 6000,
  stack: 6
  });
  </code></pre>
  
  <p>However this only outputs 'null' as the heading and 'null' as the text.</p>
  
  <p>messages.php:</p>
  
  <pre><code>&lt;?php
  
      if (!isset($_SERVER['HTTP_REFERER'])){
          die;
      }
  
      ob_start();
      require_once '../../../app/config.php';
      require_once '../../../app/init.php';
  
      if (!empty($maintaince)){
          die();
      }
  
  
      if (!($user -&gt; LoggedIn()) || !($user -&gt; notBanned($odb)) || !($user-&gt;isAdmin($odb))){
          die();
      }
  
  
      $username = $_GET['username'];
  
      if(empty($username)){
          die();
      }
  
      $SQLGetInfo = $odb -&gt; prepare("SELECT * FROM `live_messages` WHERE id = 1");
      $SQLGetInfo -&gt; execute(array($_SESSION['ID']));
      $messageInfo = $SQLGetInfo -&gt; fetch(PDO::FETCH_ASSOC);
  
      $type = $messageInfo['type'];
      $title = $messageInfo['title'];
      $message = $messageInfo['message'];
  
  
  ?&gt;
  </code></pre>
  
  <p>EDIT: If I put the PHP code which is in <code>messages.php</code> directly into the file which runs this script then it fetches from the database successfully however this is no help as i'm looking to display messages in real time.</p>
  
  <p>EDIT: 
  Full script</p>
  
  <pre><code>&lt;script&gt;
  
                  window.setInterval(function(){
  
                      var xmlhttp;
  
                      if (window.XMLHttpRequest){
  
                          xmlhttp = new XMLHttpRequest();
  
                      }
  
                      else{
  
                          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  
                      }
  
                      xmlhttp.open("GET","includes/ajax/user/general/messages.php?username=&lt;?php echo $_SESSION['username']; ?&gt;",true);
  
  
  
                      xmlhttp.send();
  
                      $.toast({
                      heading: &lt;?php echo json_encode($title); ?&gt;,
                      text: &lt;?php echo json_encode($message); ?&gt;,
                      position: 'top-right',
                      loaderBg: '#ff6849',
                      icon: &lt;?php echo json_encode($type); ?&gt;,
                      hideAfter: 6000,
                      stack: 6
                      });
  
  
                  }, 5000);
  
  
                  &lt;/script&gt;
  </code></pre>