Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
set_flashdata not working when reloading using javascript

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
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!