I’m using a form submission popup.if the form submitted success means no issue in case form submission fail we show a popup when we click on the close we form have the data what we entered so we correct the error field again we submit the button is not working. i think its an issue in generating the csrf .how can i fix this?
<script>
$(document).ready(function() {
// Handle form submission
$('#registrationForm').submit(function(e) {
e.preventDefault();
var csrfHash = '<?php echo csrf_hash(); ?>';
$.ajaxSetup({
headers: {
'X-CSRF-Token': csrfHash
}
});
// Get form data
var formData = $(this).serialize();
// Perform AJAX request
$.ajax({
url: '<?= base_url('register-post'); ?>',
type: 'POST',
data: formData,
success: function(response) {
if (response && response.success) {
// Handle successful form submission
console.log(response);
showPopup('Success!', 'Registration successful!');
setTimeout(function() {
location.reload();
}, 3000);
} else {
// Handle failed form submission
var errorMessage = response && response.message ? response.message :
'Error occurred during form submqwwission.';
showPopup('Failed!', response.message);
console.error(errorMessage);
}
},
error: function(xhr, status, error) {
// Handle AJAX error
if (csrfHash) {
console.log('ok3')
console.log(csrfHash);
} else {
console.log('faaa')
// Handle CSRF verification failure
// ...
}
showPopup('Failed!', 'Error occurred during form submission: ' + xhr
.responseText);
console.error(error);
}
});
});
});
this my function.what i need is to generate new csrf on each submit