javascript - How to redirect user if admin pushed on submit button from another page?
Get the solution ↓↓↓At first sorry for my English.
I have made an Admin panel on the page admin.php where I have a form like this:
<form action="#" method="post">
<button type="submit" name="submit">
</form>
The submit is sending a TRUE to the database and stores it there. Here comes the part where I'm stuck.
On another page user.php there is an user form like this:
<form action="loading.php" method="post">
<input type="text" name="email">
<button type="submit" name="submit">
</form>
After the user fills in the form his data also gets send to the database. After that he gets redirected to the loading.php where he is waiting on a loading screen.
The user know must wait till the admin posts TRUE to the database. This part needs to be live, so the user is waiting on the loading screen and when the admin submits his form to the database the user gets redirected live so he don't need to refresh the page. What is the best method to perform this kind of action? Can I set a timer with an interval in AJAX to check for it? And so how do I perform this?
Thanks in advance.
Answer
Solution:
PHP Side:
<?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 side (I've only done this with jquery so check this for vanilla 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);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xml parsing error: no root element found
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.