display dynamic data via selectbox [codeigniter 4]

Viewed 22

I want to displat data from selectbox when the button clicked

a. controller

    public function showComment()
        {
            $request = \Config\Services::request();
            if ($request->isAJAX()) {
    
                $kategory = $request->getVar('category');
    
    
                $sql = "SELECT * FROM category";
    
                $data = [
                    "path"            => $this->path,
                    "module"          => $this->module,
                    "userid"          => $this->userid,
                    "act"             => $kategory,
                    "data"            => [],
                    "sql"             => $sql,
                    "builder"         => $this->category->query($sql),
    
                ];
    
    
                return $this->response->setJSON(json_encode(['status' => true, "result" => view($this->path . '/' . $this->module, $data)]));
            } else {
                throw new \CodeIgniter\Exceptions\PageNotFoundException("Page Not Found!");
            }
        }

b. view

    <form action="#" data-url="<?= base_url(); ?>/<?= $path; ?>/<?= $module; ?>/showComment"
                            class="form-horizontal" method="POST" enctype="multipart/form-data" id="formData"
                            name="formData">
                            <?= csrf_field(); ?>
    
                            <div class="col-md-12">
                                <div class="row">
    
                                    <div class="col-md-4 form-group mb-1">
                                        <label class="mb-0"> Category </label>
                                        <select class="form-control form-control-sm select2" name="category" id="category">
                                            <option value="">::Pilih::</option>
                                            <?php foreach ($category as $rest) { ?>
                                            <option value="<?= $rest->id_kategori; ?>">
                                                <?= $rest->nama_kategori; ?></option>
                                            <?php } ?>
                                        </select>
                                    </div>
    
                                </div>
                            </div>
    
                            <div class="btn btn-group">
                                <hr>
                                <button type="submit" id="submit" name="submit" class="btn btn-success btn-sm">
                                    View
                                </button>
                            </div>
    
                            <div class="row">
                                <div id="view" class="col-md-12 table-responsive center"
                                    style="margin-left: auto; margin-right:auto;"></div>
                            </div>
                        </form>

c. dislaying method using case

<?php switch ($act) {
    default: ?>


    <?= $this->extend('pages'); ?>
    <?= $this->Section('content'); ?>
    
    ...
    
    <?php break;
        case "1"; ?>
    1
    
    <?php break;
        case "2"; ?>
    
    <?php break;
    } ?>

I've tried applying the related variable to the case value but the result is of course not predefined in index().

it's all because the showcomment function will be triggered when the button is pressed

0 Answers
Related