Does a form refresh the page on submission?

Script47

I have the following code for a form:

<?php
if ($user_stat->location_id === $destination->destination_id) {
    ?>
    <small class="pull-left">You are here.</small>
    <?php
} else {
    ?>
    <form class="pull-left" action="my_offices.php" method="post">
        <input type="hidden" name="office_id" value="<?= $destination->destination_id ?>" />
        <input type="hidden" name="action" value="travel" />

        <a href="javascript:void(0);" role="button" class="badge badge-primary" onclick="$(this).parent().submit();">Travel</a> <small><?= format_money(TRAVEL_COST) ?></small>
    </form>                            
    <?php
}
?>

When the link is clicked, it submits the form which in turn causes the following code to execute,

if (!Request::check_post('office_id')) {
    echo UI::alert('Sorry, a malformed request was sent.', 'danger');
} else if (!Office::exists(Request::get_post('office_id'))) {
    echo UI::alert('Sorry, this <strong>Office</strong> does not exist.', 'danger');
} else if (!UserOffice::owns($user->id, Request::get_post('office_id'))) {
    echo UI::alert('Sorry, you don\'t own this <strong>Office</strong>.', 'danger');
} else if (Request::get_post('office_id') === $user_stat->location_id) {
    echo UI::alert('Sorry, you are currently situated in this <strong>Office</strong>.', 'danger');
} else {
    $office_id = Sanitize::int(Request::get_post('office_id'));
    $total = $user_stat->money - TRAVEL_COST;

    if ($total < 0) {
        echo UI::alert('Sorry, you don\'t have enough money to travel.', 'danger');
    } else if (UserOffice::travel($user->id, $office_id)) {
        $minus = Database::run('UPDATE user_stat SET money = ? WHERE id = ?', [$total, $user->id]);

        if ($minus) {
            echo UI::alert('You travelled to your <strong>' . Office::get($office_id)->name . '</strong> office.', 'success');
        }
    }
}

The important point to is UserOffice::travel($user->id, $office_id) which updates a field in a table matching to the respective $office_id. The following should clarify what is meant.

Note: The changes are reflected in the database, I have confirmed.

I am currently in 'New York' which has an $office_id of 3,

Notice the 'London' card shows the 'Travel' link whereas the 'New York' card shows the text as defined by the aforementioned if statement.

The Issue

What the intended effect should be that when you click 'Travel' it should switch the texts on form submission, however, that doesn't seem to happen. It only happens when I refresh the page (not through a form submission). The following GIF should clarify what I mean. Notice how it didn't switch the badges of 'Travel' and the 'Your are here.' text. Now, when I explicitly refresh the page you can see the change take effect.

What causes the changes to not take effect?

How do form submissions work as opposed to page refreshing?

Elentriel

I will presume from your code here that

$user_stat

variable is already instantiated by the time you try to save to your database, which in turn you use to print your form.

In short, what happens here is:

get $user_stat from database and put it in a variable

change $user_stat in the database

Try to use unchanged version of $user_stat

Before the last step, you should either re-select $user_stat from teh database, or change its value with code, if you want your changes to be visible when the last step comes

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Stopping form submission on page refresh

WordPress: Content in foreach remains on page because it does not refresh on form submission | Reload array after form submission?

Redirect and refresh page on form submission in React frontend

PHP Auto refresh page after another page form submission

Having to refresh a page after form submission using jquery ajax

JQuery form submission without page refresh not working due to JQuery conflict

How to auto refresh previous page from popup form submission

form contact page submission

function confirm() should trigger only after valid form submission not on page refresh

How to allow page refresh/redirect after Ajax POST submission (of Django form)

Component is not re-rendering on form submission. It only re-renders on a full page refresh

Form submission through ajax and reply from servlet through JSON without page refresh

Prevent page reload on form submission

Form submission opens php page

Webpage UI not updating on refresh after form submission

Ajax form submission without refresh "not working"

AJAX form submission to MYSQL without refresh

Form submits on page refresh

Form submission does not work with validationSchema

JS does not detect form submission

Form Validation Works But Does Not Prevent Form Submission

Form submission using ajax and page view moderation after the submission

Preventing page reload upon form submission to database

laravel form submission showing 419 page expired

Flask: redirect to same page after form submission

php return to page after form submission

Flask form submission without Page Reload

Redirect another page after successful form submission

HTML Form Submission to Existing Web Page

TOP Ranking

HotTag

Archive