I’m creating a single page form that won’t reload the page after submitting the form. I’m currently at the stage where I first get messages like I filled something in incorrectly (that’s ok), but then when I click the button again, I get err 419. I noticed, in Applications DevTool, that the first time both XSRF-TOKEN and laravel_session change, but the next time I submit the form, only laravel_session changes.How to prevent it and how to fix it? Thank you
Step by Step:
-
I come to the page, of course I have a new laravel_sesion + XSRF token
-
I click on the form – a new laravel_session + XSRF token is then generated
-
I click on the form again – only laravel_session is generated
@section('content')
@include('notifications')
@if(config('register.register'))
<div style="text-align: center">
{!! Html::form('POST', route('auth.register.register'))->id('registerForm')->class('form-horizontal')->open() !!}
@if($hash)
<div class="form-group">
{!! Html::label('reflink_hash', trans('translate.promo_code_label_user'))->class('control-label col-sm-3')->attribute('data-translate-key', 'promo_code_label') !!}
<div class="col-sm-8">{!! Html::text('reflink_hash', $hash)->class('form-control')->readonly()->attribute('data-translate-key', 'promo_code_placeholder_user') !!}</div>
</div>
@endif
<div class="form-group">
{!! Html::label('username', trans('translate.login'))->class('control-label col-sm-3')->attribute('data-translate-key', 'login') !!}
<div class="col-sm-8">{!! Html::text('username')->class('form-control')->attribute('data-translate-key', 'login_placeholder') !!}</div>
</div>
<div class="form-group">
{!! Html::label('password', trans('translate.password'))->class('control-label col-sm-3')->attribute('data-translate-key', 'password') !!}
<div class="col-sm-8">{!! Html::password('password')->class('form-control')->attribute('data-translate-key', 'password_placeholder') !!}</div>
</div>
<div class="form-group">
{!! Html::label('password_confirmation', trans('translate.confirm_password'))->class('control-label col-sm-3')->attribute('data-translate-key', 'confirm_password') !!}
<div class="col-sm-8">{!! Html::password('password_confirmation')->class('form-control')->attribute('data-translate-key', 'password_placeholder') !!}</div>
</div>
<div class="form-group">
{!! Html::label('email', trans('translate.email'))->class('control-label col-sm-3')->attribute('data-translate-key', 'email') !!}
<div class="col-sm-8">{!! Html::email('email')->class('form-control')->attribute('data-translate-key', 'email_placeholder') !!}</div>
</div>
<div class="form-group">
{!! Html::label('code', trans('translate.code_delete_char'))->class('control-label col-sm-3')->attribute('data-translate-key', 'code_delete_char') !!}
<div class="col-sm-8">{!! Html::text('code')->class('form-control')->attribute('maxlength', '7') !!}</div>
</div>
<div class="form-group">
{!! Html::label('pin', trans('translate.pin'))->class('control-label col-sm-3')->attribute('data-translate-key', 'pin') !!}
<div class="col-sm-8">{!! Html::text('pin')->class('form-control')->attribute('maxlength', '4')->attribute('data-translate-key', 'pin_placeholder') !!}</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-8" style="width: 304px; margin:0 auto 10px auto">
{!! Html::label('captcha', trans('translate.captcha'))->class('control-label col-sm-3')->attribute('data-translate-key', 'captcha') !!}
{!! app('captcha')->display() !!}
</div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" name="checkbox">
<span data-translate-key="terms">{{ trans('translate.accept_terms') }}</span>
<a href="{{ route('main.rules.index') }}"><b data-translate-key="tos">{{ trans('translate.tos') }}</b></a>
<span data-translate-key="terms">{{ trans('translate.terms_end') }}</span>
</label>
</div>
</div>
<div class="form-group">
<button type="submit" name="create-account" value=" "><span data-translate-key="register">{{ trans('translate.register_me') }}</span></button>
</div>
{!! Html::form()->close() !!}
</div>
@else
<div class="alert" data-translate-key="registration_unavailable">{{ trans('translate.registration_unavailable') }}</div>
@endif
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('#registerForm').on('submit', function(event) {
event.preventDefault(); // Zabrání obnovení stránky
$.ajax({
url: $(this).attr('action'),
method: 'POST',
data: $(this).serialize(),
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(response) {
if (response.status === 'success') {
alert(response.message); // Úspěšná registrace
// Přesměrování nebo aktualizace UI
} else {
alert(response.message); // Zobrazení chyby
}
},
error: function(xhr) {
alert('Err');
}
});
});
});
</script>
@endsection