Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sessions are unnecessary. Your basic plan should be:</p> <ul> <li>Get variables from $_POST()</li> <li>Validate form values.</li> <li>Create an array with the values that passed validation.</li> <li>Use the array to fill the values when redirected to the form.</li> </ul> <p>Before I started using CodeIgniter, I used to use <code>if</code> statements to validate and generate error messages like this:</p> <pre><code>if ( ! empty($_POST['email'])){ if ($this-&gt;verifyInput($_POST['email'], 6)){ $fill['email'] = $_POST['email'];//$fill is the array of values that passed validation } else $tally++;//$tally keeps a running count of the total number of erors } </code></pre> <p>I make a function call to <code>$this-&gt;verifyInput</code> which accepts the field value and the type of validation to perform--in this case it's 6, which indicates email. Here's my validation function:</p> <pre><code>function verifyInput($input, $type){ if ($type == 0) $pattern = '/^1$/';//just the number 1 elseif ($type == 1) $pattern = '/^[0-9]+$/';//just integers elseif ($type == 2) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü]+$/';//just letters elseif ($type == 3) $pattern = '/^[0-9A-Za-zÁ-Úá-úàÀÜü]+$/';//integers &amp; letters elseif ($type == 4) $pattern = '/^[A-Za-zÁ-Úá-ú0-9àÀÜü\s()\/\'":\*,.;\-!?&amp;#$@]{1,1500}$/';//text elseif ($type == 5) $pattern = '/^[A-Za-zÁ-Úá-úàÀÜü0-9\']+[A-Za-zÁ-Úá-úàÀÜü0-9 \'\-\.]+$/';//name elseif ($type == 6) $pattern = '/^.+@[^\.].*\.[a-z]{2,}$/';//e-mail elseif ($type == 7) $pattern = '/^((\(0?[1-9][0-9]\))|(0?[1-9][0-9]))?[ -.]?([1-9][0-9]{3})[ -.]?([0-9]{4})$/';//brazilian phone if (preg_match($pattern, $input) == 1) return true; else return false; } </code></pre> <p>Once the validation is done, I use the <code>$fill</code> array to prefill the form for each field:</p> <pre><code>&lt;input class="field" type="text" name="email" value = "&lt;?php echo $fill['email']; ?&gt;"/&gt; </code></pre> <p>You should also make sure that <code>$fill</code> is iniciated with empty values such as <code>$fill['email']='';</code> or check to make sure the values are set before filling the input tags:</p> <pre><code>&lt;input class="field" type="text" name="email" value = "&lt;?php if(isset($fill['email'])) echo $fill['email']; ?&gt;"/&gt; </code></pre> <p>These are just general guidelines that you can adapt to your needs, but I found this to be a convenient way to handle it.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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