Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's some elaboration on zombat's solution with code examples.</p> <p>I usually have the form post to the same controller/method. Then I let $this->form_validation->run() handle all the dirty work. If $_POST data is present, the validator checks the rules. If it passes I redirect them to a success page, otherwise the same page is presented with validation errors displayed in the repopulated form.</p> <pre><code>class Contact extends CI_Controller { function __construct() { parent::__construct(); } function index() { /* the foreach() line makes each field visible to form_validation so we can reliably repopulate (using set_value, set_checkbox, etc) fields that have no validation rules example: since 'phone' has no rules, we would not be able to repopulate that field if validation failed */ foreach ($_POST as $key =&gt; $value) $this-&gt;form_validation-&gt;set_rules($key, '', ''); $this-&gt;form_validation-&gt;set_rules('first_name', 'First Name', 'required|min_length[2]'); $this-&gt;form_validation-&gt;set_rules('last_name', 'Last Name', 'required|min_length[2]'); $this-&gt;form_validation-&gt;set_rules('email', 'Email Address', 'required|strtolower|valid_email'); if ($this-&gt;form_validation-&gt;run() == FALSE) { $this-&gt;load-&gt;view('contact_form'); } else { /* save form data, email it, etc then redirect the user so they cannot re-submit the same data */ redirect('contact/success'); } } function success() { $this-&gt;load-&gt;view('contact_success'); } } </code></pre> <p>Example HTML for application/views/contact_form.php</p> <pre><code>&lt;form method="post" action="&lt;?php echo current_url(); ?&gt;"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;First Name&lt;/td&gt; &lt;td&gt;&lt;?php echo form_input('first_name', set_value('first_name')); ?&gt; &lt;?php echo form_error('first_name'); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Last Name&lt;/td&gt; &lt;td&gt;&lt;?php echo form_input('last_name', set_value('last_name')); ?&gt; &lt;?php echo form_error('last_name'); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Email Address&lt;/td&gt; &lt;td&gt;&lt;?php echo form_input('email', set_value('email')); ?&gt; &lt;?php echo form_error('email'); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Phone Number&lt;/td&gt; &lt;td&gt;&lt;?php echo form_input('phone', set_value('phone')); ?&gt; &lt;?php echo form_error('phone'); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;td&gt;&lt;button type="submit"&gt;Send Message&lt;/button&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>I have absolutely no worries about CSFR or XSS doing it this way.</p> <p><sub>Note: As with any publicly accessible form, there are always going to be junk submissions (from bots AND real people trying to solicit business). If you encounter a lot of them, fine-tune your validation rules accordingly.</sub></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload