如何在CodeIgniter中从视图向控制器提交静态内容
Hey there! Let's walk through the common, straightforward ways to send static HTML content from your frontend to a CodeIgniter controller—no overly complex tricks required, just practical implementations tailored to different needs.
Method 1: HTML Form Submission (Full Page Refresh)
This is the simplest approach if you don't mind a page reload after submission. You’ll wrap your static content in a form, point it to your controller method, and send the data via POST (preferred for security) or GET.
Step 1: Build the HTML Form
Use a hidden input (for short snippets) or a textarea (for longer HTML blocks) to hold your static content. Make sure to target your controller correctly using CodeIgniter’s base_url() helper:
<form action="<?php echo base_url('your_controller/handle_static_content'); ?>" method="POST"> <!-- For short static content --> <input type="hidden" name="static_html" value="<p>Your static HTML snippet here</p>"> <!-- OR for longer, multi-line content --> <textarea name="static_html" style="display:none;"> <div class="static-block"> <h3>My Static Content</h3> <p>Longer HTML content goes here</p> </div> </textarea> <button type="submit">Submit Content</button> </form>
Pro tip: If you use CodeIgniter’s form helper, replace the form tag with <?php echo form_open('your_controller/handle_static_content'); ?>—it automatically adds CSRF protection, which is critical for security.
Step 2: Process the Data in Your Controller
In your controller file (your_controller.php), create the handle_static_content method to retrieve and work with the submitted content:
public function handle_static_content() { // Fetch the submitted static HTML $static_content = $this->input->post('static_html'); // Process the content (save to DB, parse, etc.) if (!empty($static_content)) { // Example: Pass to a model for storage // $this->content_model->save_static_content($static_content); echo "Static content received successfully!"; } else { echo "No content was submitted."; } }
Method 2: AJAX Submission (No Page Refresh)
If you want to submit content without reloading the page, AJAX is your go-to. Here’s a jQuery-based example:
Step 1: HTML & JavaScript Trigger
Add a button to trigger the submission and the AJAX script to your page:
<button id="submit-static-btn">Submit Static Content (No Refresh)</button> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function() { $('#submit-static-btn').click(function() { // Define your static HTML content const staticHtml = '<div class="custom-static"><p>This is my static content to send via AJAX</p></div>'; // Send the request $.ajax({ url: '<?php echo base_url('your_controller/handle_static_content'); ?>', type: 'POST', data: { static_html: staticHtml, // Include CSRF token (required if CodeIgniter's CSRF protection is enabled) <?php echo $this->security->get_csrf_token_name(); ?>: '<?php echo $this->security->get_csrf_hash(); ?>' }, success: function(response) { alert('Content submitted! Server response: ' + response); }, error: function() { alert('Oops, something went wrong with the submission.'); } }); }); }); </script>
Step 2: Controller Handling (Same as Method 1)
The controller method stays identical to the one in Method 1—you’ll still retrieve the content with $this->input->post('static_html') and process it as needed.
Critical Security Reminders
- Always use
POSTinstead ofGETto avoid exposing content in the URL. - Keep CodeIgniter’s CSRF protection enabled (it’s on by default in newer versions) to block cross-site request forgery attacks.
- If your "static" content might ever include user input, sanitize it using
$this->security->xss_clean()to prevent XSS vulnerabilities.
内容的提问来源于stack exchange,提问作者Sangram Tanugade

