Why does Website layout load after finishing PHP process

105 Views Asked by At

I am creating a webpage which works with serial port. I am now having an issue now whereby it only loads after completing the PHP process. Below is the code.

<html>
<body>
    div class="container">
        <div id="container">
            <div id="navigation">
                <h1 style="text-align:center; color:#00B4CC;">Pharmacy Counter</h1>
            </div>
            <div id="box" style="border: 1px solid #00B4CC; text-align:center;">
                <form style="padding-top: 20px;">
                    <fieldset>
                        <h4>Patient Details</h4>
                        <h4>Name:</h4>
                        <h4>DOB:</h4>
                        <h4>Address:</h4>
                    </fieldset>        
                </form>
            </div>




<?php

        $portName = 'com9:';
        $baudRate = 57600;
        $bits = 8;
        $spotBit = 1;

        require_once("dbconn.php");
        include("arduino.php");
        $db = getConnection();


        function echoFlush($string)
        {
            echo $string . "<br>";
            flush();
            ob_flush();
        }

        if(!extension_loaded('dio'))
        {   
                //dl('php_dio.dll');
            echoFlush( "PHP Direct IO does not appear to be installed for more info see: http://www.php.net/manual/en/book.dio.php" );
            exit;
        }

        try 
        {
            //the serial port resource
            $bbSerialPort;


            //echoFlush(  "Connecting to serial port: {$portName}" );

            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') 
            { 
                echo "hi Windows\n";
                //dio_close($bbSerialPort);
                //$bbSerialPort = dio_open($portName, O_RDWR);
                exec("mode {$portName} baud={$baudRate} data={$bits} stop={$spotBit} parity=n xon=off to=on");  
                $bbSerialPort = dio_open($portName, O_RDWR);


            //  dio_open($portName, O_RDWR);
                echo '<br>'.'successfull exec'.'</br>';
            } 

            if(!$bbSerialPort)
            {
                echoFlush( "Could not open Serial port {$portName} ");
                exit;
            }
            $a=0;
            while(($a++)<100){
               dio_read($bbSerialPort);

            }

         ?>
 </body>
 </html>

This is the code of both PHP and html. Problem now is the layout loads after running the php script. I have no idea why this is happening.

1

There are 1 best solutions below

0
On

Apache renders the whole PHP file at once, a solution would be for you to make a AJAX call to another page which have the connect to serial port.

A ajax call can be made this way (requires jQuery) :

  $(document).ready(function(){
    $.ajax({
       url: '...',
       success: function(response) {
           $('#ajaxReciever').html(response);
       }
    });
});

The only thing you need on your html page is a container with the ID ajaxReciever (which can have a nice font awesome spinner first :))