如何将select组件数据传递至控制器?功能实现遇阻求助
Let's break down the issues in your code step by step—there are a few simple fixes needed to get your category filter working:
1. Syntax Error in <option> Tags
Your option elements have a typo in the value attribute: you wrote value'...' instead of value="...". This makes the value attribute invalid, so no data gets sent when the form submits.
Fix it like this:
<option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option>
2. Missing echo for form_close()
In CodeIgniter, form_close() returns the closing </form> tag as a string, but you're not echoing it. This means your form isn't properly closed, which can break form submission. Always echo it to render the closing tag:
<?php echo form_close(); ?>
3. No Way to Submit the Form
By default, a <select> element won't submit the form on its own. You have two practical options here:
Option A: Add a Submit Button
Add a button inside the form to let users trigger the submission manually:
<button type="submit">Apply Filter</button>
Option B: Auto-Submit on Select Change
Use JavaScript to automatically submit the form when the user selects a category. Add this script right before closing the form:
<script> document.querySelector('select[name="category"]').addEventListener('change', function() { this.form.submit(); }); </script>
4. Controller Adjustments (For Your End Goal)
Your current myFunction() is just echoing the value, but to load the new model/data and stay on the same view, you'll want to modify it to fetch filtered data and pass it back to your view. Here's an example:
public function myFunction() { // Get the selected category ID (use post or get depending on your form method) $category_id = $this->input->post('category'); // Load your model $this->load->model('Category_model'); // Fetch filtered data using the category ID $data['filtered_results'] = $this->Category_model->get_items_by_category($category_id); // Pass the category list again for the select dropdown $data['cat'] = $this->Category_model->get_all_categories(); // Load your view with the filtered data $this->load->view('your_view_name', $data); }
If you prefer to use GET requests (so the filter appears in the URL, making bookmarkable links), update your form open to use GET:
<?php echo form_open('Welcome/myfunction', ['method' => 'get']); ?>
Then fetch the value with $this->input->get('category') instead of post.
Putting it all together, your fixed view would look like this:
<?php echo form_open('Welcome/myfunction'); ?> <select name="category"> <option value="">filter</option> <?php foreach ($cat->result() as $row): ?> <option value="<?php echo $row->id; ?>"><?php echo $row->name; ?></option> <?php endforeach; ?> </select> <button type="submit">Apply Filter</button> <script> // Optional auto-submit on selection document.querySelector('select[name="category"]').addEventListener('change', function() { this.form.submit(); }); </script> <?php echo form_close(); ?>
内容的提问来源于stack exchange,提问作者Oriol

