我在网址https://akshayshrivastav.me/index.php
我正在做的是在Mozilla浏览器上保持此URL保持打开状态。
我正在做的是我从Chrome浏览器向该网址发送帖子请求现在发生的事情是我有以下代码
<?php
if($_POST)
{
print_r($_POST);
}
else
{
echo "No Request Received! Please Check the Request You Are Trying To Make!";
}
?>
以上代码将在chrome浏览器上运行,因为当表单f = data发布到此url时,页面将显示已发布的内容。
但是在mozilla浏览器中,我想要的是即使数据是通过Chrome浏览器提交的,只要对后面的url发布帖子或获取请求并显示发布的值,它就应该在mozzila浏览器中动态刷新页面。
请帮助逻辑。
答案 0 :(得分:3)
我正在使用表格代码:
CREATE TABLE `items` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`time` int(10) NOT NULL,
PRIMARY KEY (id)
)
我已将所有内容都写在1个文件中,但您应该至少分开表单。简短版本:每隔X秒/分钟从jQuery发送POST
请求并检查返回值。如果我们得到X值,我们重新加载页面,否则,继续发送请求。
我评论了一些内容,以便更好地理解为什么我这样写。它可能并不完美(我可能已经错过了一些东西),但总的来说它工作(测试过):
<?php
$db = new mysqli('localhost', 'root', 'password', 'test_database');
/* Must connect to database first, otherwise it's no use */
if ($db->connect_errno) {
exit('Connection failed because: '.$db->connect_error);
}
/* Check if we received $_POST from jQuery script */
if (isset($_POST['lastRow'])) {
$query = $db->query('SELECT id FROM items ORDER BY id DESC LIMIT 1');
$result = $query->fetch_array(MYSQLI_ASSOC);
if (is_null($result)) {
$lastRow = 0;
} else {
$lastRow = $result['id'];
}
/* Return 1 if DB was changed, otherwise, return 0 */
if ($_POST['lastRow'] != $lastRow) {
echo 1;
} else {
echo 0;
}
/* We don't need to print entire HTML code */
exit;
}
/* Add new item through $_POST */
if (!empty($_POST['item_name'])) {
$item = $db->real_escape_string($_POST['item_name']);
$query = $db->query('INSERT INTO items (name, time) VALUES("'.$item.'", '.time().')');
if ($query) {
echo 'Item successfully added!<br>';
} else {
echo 'Item was not successfully added because: '.$db->error.'<br>';
}
}
?>
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<title>Page with items list</title>
</head>
<body>
<?php
$query = $db->query('SELECT id FROM items ORDER BY id DESC LIMIT 1');
$result = $query->fetch_array(MYSQLI_ASSOC);
/* If table is empty, give 0 rows, otherwise, take last ID */
if (is_null($result)) {
$lastRow = 0;
} else {
$lastRow = $result['id'];
}
?>
<!-- FORM AREA -->
<div id="form_div" class="col-lg-2 form-group">
<form action="" method="post">
<label for="item_name">Item name:</label>
<input type="text" id="item_name" name="item_name" class="form-control" placeholder="Enter item name">
<input type="submit" class="btn btn-primary" value="Insert">
</form>
</div>
<!-- ROW PRINTING AREA -->
<div id="table_div" class="col-log-12">
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Creation time</th>
</tr>
<?php
$query = $db->query("SELECT * FROM items");
while ($result = $query->fetch_array(MYSQLI_ASSOC)) {
?>
<tr>
<td><?php echo $result['id']; ?></td>
<td><?php echo $result['name']; ?></td>
<td><?php echo date('Y-m-d', $result['time']); ?></td>
</tr>
<?php
}
?>
</table>
</div>
<!-- Launching script when document is loaded -->
<script type="text/javascript">
$(document).on('ready', function(e) {
function checkData() {
/* Sending POST HTTP request */
$.post('consi.php', { lastRow: <?php echo $lastRow; ?> }, function(data) {
if (data == 1) {
/* OK, last row was changed, so just reload the page to see new results */
window.location.href = window.location.href;
}
});
}
/* 1000 -> 1 second */
setInterval(checkData, 5000);
});
</script>
</body>
</html>
答案 1 :(得分:1)
请在index.php文件中尝试以下代码
<meta http-equiv="refresh" content="30">
其中值30是时间。