I am working on Laravel 5.5 framework. I have a form home page like this:
<form action = "/result" method = "post">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token() ?>">
<table>
<tr>
<td>Name or Nickname</td>
<td><input type = "text" name = "name_nickname" autofocus /></td>
</tr>
<tr>
<input type = "submit" value = "LuckyNumber" />
</td>
</tr>
</table>
The controller looks like this:
class SixGetController extends Controller
{
public function luckyNumber(Request $request){
$nameNickname = $request->input('name_nickname');
$luckyNumber = rand (1,10);
DB::table('visitor')->insert(
['name_nickname' => $nameNickname, 'luckyNumber' => $luckyNumber]);
return view('result', ['nameNickname' => $nameNickname, 'luckyNumber' =>
$luckyNumber]);
}
The result page looks like this:
<p><?php echo $nameNickname; ?> </p>
<p>Your lucky number is <?=$result?> .</p>
If the user presses the reload F5 button the scrip will reroll the random number generator and resubmit the data with the rerolled number. I've read about the PGR pattern which i dont know how to use and something about manipulating history which i dont understand either. Can somebody point out what kind of code do i put somewhere to prevent the reroll and the resubmission. Thanks.
For laravel implementation, you can use Session Flash Data.
Sometimes you may wish to store items in the session only for the next request. You may do so using the flash method. Data stored in the session using this method will only be available during the subsequent HTTP request, and then will be deleted. Flash data is primarily useful for short-lived status messages:
In this case, when someone make a post request, you should store the useful data to session, and redirect them to other route. The other route can then retrieve the useful data to display to the view, and user no longer resubmit the form when refresh the page.
public function luckyNumber(Request $request) {
...
$request->session()->flash('nameNickname', $nameNickname);
$request->session()->flash('luckyNumber', $luckyNumber);
return redirect()->action('SixGetController@resultView');
}
public function resultView(Request $request) {
$nameNickname = $request->session()->get('nameNickname');
$luckyNumber = $request->session()->get('luckyNumber');
return view('result', ['nameNickname' => $nameNickname, 'luckyNumber' => $luckyNumber]);
}