php长轮询与提交表单

时间:2017-12-16 06:33:46

标签: php real-time long-polling

这是线程implementing php long polling

的扩展问题

我得到了解决方案。但是在我的server.php中,我正在显示一个几乎没有输入字段的表单。这应该显示在#response区域。现在我能够立即显示相同的内容。由于它以秒为单位加载,我无法在任何输入框中输入任何内容。这是我的显示页面

<?php
ob_start();
include('includes/sessions.php');
include('includes/config.php'); 
include('includes/functions.php');
$grp_id = $_GET['id'];
?>
<!DOCTYPE html>
    <head>
       <!-- head section -->
        <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<script type="text/javascript">
function getContent(timestamp)
{
    var queryString = {'timestamp' : timestamp,  'id' : <?php echo $grp_id; ?>};

    $.ajax(
        {
            type: 'GET',
            url: 'http://localhost/folder/server.php',
            data: queryString,
            success: function(data){
                // put result data into "obj"
                var obj = JSON.parse(data);
                // put the data_from_file into #response

                $('#response').html(obj.data_from_file);
                // call the function again, this time with the timestamp we just got from server.php
                getContent(obj.timestamp);
            }
        }
    );
}

// initialize jQuery
$(function() {
    getContent();
});

</script>

    </head>

    <body<?php if ($body_classes) { echo ' class="' . $body_classes . '"'; } ?>>
<div id="page-content" class="block">
    <div class="row gutter30">
        <div class="col-xs-12">
        <div id="response"></div>

        <!--Contents in server.php will display according to the condition changes in database-->

</div>
</div>
</div> 

server.php

<?php
set_time_limit(0);
$data_source_file = 'data.txt';
include('includes/sessions.php');
include('includes/config.php'); 
include('includes/functions.php');

// main loop
while (true) {

    // if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
    $last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;

    // PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
   // clearstatcache();
    // get timestamp of when file has been changed the last time
   // $last_change_in_data_file = filemtime($data_source_file);

         // Create database
       $sql = "SELECT max(gid) FROM base_grp WHERE gid=".$_GET['id']."";
        $result = mysqli_query($conn, $sql);

        $last_change_in_data_file = mysqli_fetch_array($result, MYSQLI_NUM)[0]; 
    // if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
    if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {

      $sql = "SELECT is_set FROM base_grp WHERE gid =".$_GET['id']."";
        $result = mysqli_query($conn, $sql);

        if (mysqli_num_rows($result) > 0) {
            // output data of each row
            while($row = mysqli_fetch_assoc($result)) {
               if($row['is_set'] == 0)
               {
                $data .= '<form name="frm" method="post" action="show_data.php">
    <div class="row gutter30" style="margin-top:20px;">  
        <div class="form-group">
        <img src="img/img1.png" class="image" /> 
<input type="text" name="img1" class="form-control"  />
<input type="hidden" name="img1_id" value="3" />
</div>

<div class="form-group"> 
       <img src="img/img2.png" class="image" /> 
<input type="text" name="img2" class="form-control" />
<input type="hidden" name="img2_id" value="4" />
</div>

<div class="form-group"> 
       <img src="img/img3.png" class="image" /> 
<input type="text" name="img3" class="form-control" />
<input type="hidden" name="img3_id" value="2" />
</div>

<div class="form-group"> 
       <img src="img/img4.png" class="image" /> 
<input type="text" name="img4" class="form-control"  />
<input type="hidden" name="img4_1" value="1" />
</div>

<button type="submit" name="lock" class="btn btn-sm btn-success">SUBMIT</button>
</div>

</form> ';  
$last_change_in_data_file = $row["gid"];  
               }
              else  if($row['is_set'] = 1)
               {
        $data .=  "<div class='row gutter30' style='margin-top:20px;'>
        <div class='col-xs-3'>
        <img src='img/img1.png' class='image' /> </div>
        <div class='col-xs-3'>
        <img src='img/img2.png' class='image' /> </div>
        <div class='col-xs-3'>
        <img src='img/img3.png' class='image' /> </div>
        <div class='col-xs-3'>
        <img src='img/img4.png' class='image' /> </div>
        </div>
";
                $last_change_in_data_file = $row["gid"];
               }
                $last_change_in_data_file = $row["gid"];
            }
        }
       // mysqli_close($conn);

        // put data.txt's content and timestamp of last data.txt change into array
        $result = array(
            'data_from_file' => $data,
            'timestamp' => $last_change_in_data_file
        );
        mysqli_close($conn);
        // encode to JSON, render the result (for AJAX)
        $json = json_encode($result);
        echo $json;

        // leave this loop step
        break;

    } else {
        // wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
        sleep( 1 );
        continue;
    }
}

没有得到如何进入... 有人可以建议我哪里出错了吗?谢谢!

0 个答案:

没有答案