title: how to fetch data from sql server database in php without refreshing the page
body: <p>I am trying to get some data from the database.  I create a function that is located in <code>functions.php</code> file that return a value.  On another page, I create a variable and just get that value.  I was trying to use the <code>onkey</code> to check the database but then I realize that i need to know the amount of tickets even if they don't type anything.  </p>
  
  <p>Here is the function:</p>
  
  <p><strong>function.php</strong></p>
  
  <pre><code>function is_ticket_able($conn){
  
  
  $query = "select number_of_tickets from [dbo].[TICKETS] " ;
  
  
   $stmt = sqlsrv_query($conn, $query);
  
  
   while ($row = sqlsrv_fetch_array($stmt)) {
       $amount_of_tickets = $row['number_of_tickets'];
  
   }
  
     return $amount_of_tickets;
  
   }
  </code></pre>
  
  <p>And, I am trying to check the database (without refreshing the page) and get the value on this page:</p>
  
  <p><strong>application.php</strong></p>
  
  <pre><code>$amount_of_tickets = is_ticket_able($conn);
  </code></pre>
  
  <p>Then, I just check that <code>$amount_of_tickets</code> is not 0 or 1.  Because if is one then some stuff have to change. </p>
  
  <p>I am doing this (inside application.php):</p>
  
  <pre><code>if($amount_of_tickets !=0){
     //show the form and let them apply for tickets.
  
       //also
       if($amount_of_tickets == 1){
          //just let them apply for one ticket.
       }
  }
  </code></pre>
  
  <p>EDIT:  I saw that AJAX would be the right one to use, but I am so confuse using it.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p><strong>function.php</strong></p>
  
  <pre><code>function is_ticket_able($conn){
  
  
  $query = "select number_of_tickets from [dbo].[TICKETS_LKUP] " ;
  
  
   $stmt = sqlsrv_query($conn, $query);
  
  
   while ($row = sqlsrv_fetch_array($stmt)) {
  
  
      $ticket = $row['number_of_tickets'];
  
     }
  
     return $ticket;
  }
  </code></pre>
  
  <p><strong>application.php</strong> </p>
  
  <pre><code>$amount_of_tickets = is_ticket_able($conn);
  
  &lt;script type="text/javascript"&gt;
   var global_isTicketAble = 0;
  
   checkTicket();
  
   function checkTicket()
   {
      $.ajax(
         {
          url: "application.php",
          method: 'GET',
          dataType: 'text',
          async: true,
          success: function( text )
          {
              global_isTicketAble = text;
              alert(global_isTicketAble);
              if( global_isTicketAble == 0 ){
                          window.location.replace("http://www.google.com");
              }
              setTimeout( checkTicket, 5000 ); // check every 5 sec
          }
      });        
     }
  
  
  
     &lt;/script&gt; 
  </code></pre>
  
  <p>So, now the problem is that when I <code>alert(global_isTicketAble);</code> it doesn't alert the value from the database but it does alert everything that is inside <strong>application.php</strong>...Help plzzz</p>