I'm working on a project using codeigniter 3 to send data to a function using POST fetch where it would set a flashdata in the end. but the flashdata doesn't show when I reloaded the page using javascript. why is that?
this is my code:
HTML & javascript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php pre($this->session->keep_flashdata('asd') ?: 'empty');?>
<form method="post" action="" id="form1">
<input type="hidden"
id="csrf"
name="<?=$this->data['csrf_name'];?>"
value="<?=$this->data['csrf'];?>"
/>
<input type="text" name="input1" />
<input type="submit" id="btnSubmit" value="Kirim">
</form>
<script>
const btnSubmit = document.querySelector('#btnSubmit');
btnSubmit.addEventListener('click', function(e) {
e.preventDefault();
const myform = document.querySelector('#form1');
let fd = new FormData(myform);
fetch('/test/test11', {
method: 'POST',
body: fd
})
.then(response => response.json())
.then(res => {
console.log('res', res);
if (res.status) {
window.location.href = window.location.href;
}
})
});
</script>
</body>
</html>
PHP
public function test11($param = '')
{
$this->load->library('My_validations');
$conf_uploader = $this->config->load('uploader_settings', true);
if ($this->input->server('REQUEST_METHOD') == 'POST') {
$this->session->set_flashdata('asd', 'qwerty');
$response = [
'status' => 1,
'msg' => 'asdasd',
];
echo json_encode($response);
return;
}
$this->data['conf_uploader'] = $conf_uploader;
$this->load->view('myform', $this->data);
}
// end public function test11