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.
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?
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.
Comments