如果管理员从另一个页面按下提交按钮,如何重定向用户



起初对我的英语感到抱歉。

我在Admin.php页面上做了一个管理面板,我有一个这样的表格:

<form action="#" method="post">
<button type="submit" name="submit">
</form>

提交正在向数据库发送TRUE并将其存储在那里。我陷入困境的部分来了。

在另一个页面user.php上,有一个用户表单,如下所示:

<form action="loading.php" method="post">
<input type="text" name="email">
<button type="submit" name="submit">
</form>

用户填写表单后,他的数据也会被发送到数据库。之后,他被重定向到loading.php,在那里他正在等待加载屏幕。

用户知道必须等到管理员将TRUE发布到数据库。这部分需要是实时的,所以用户在加载屏幕上等待,当管理员将表单提交到数据库时,用户会被实时重定向,这样他就不需要刷新页面了。执行这种操作的最佳方法是什么?我可以在AJAX中设置一个有间隔的计时器来检查它吗?那么我该怎么做呢?

提前谢谢。

PHP端:

<?php // checkstatus.php
$user_id = $_GET['user_id'];
assert(is_numeric($user_id));
// Whatever query engine you use.
$query = 'select redirect where user_id = %user_id';
$results = runQuery($query, array('%user_id' => $user_id);
$redirect = $results->getvalue('redirect');
$response = array(
'redirect' => $redirect,
);
header('Content-Type: application/json');
echo json_encode($response, JSON_PARTIAL_OUTPUT_ON_ERROR);
die();

Javascript方面(我只用jquery做过这件事,所以请检查一下香草https://www.sitepoint.com/guide-vanilla-ajax-without-jquery/):

function checkRedirect()
{
$.Get('/checkstatus.php', {user_id: 10}, function(response)
{
if (response['redirect'])
{
window.location.href = '/newpage.php';
}
}, 'json').error(function()
{
// Something broke notify the user somehow. Please don't use alert.
clearInterval(interval);
});
}
// Must set this in global scope or interval will not be accessible inside error.add
interval = setInterval(checkredirect, 5000);

最新更新