PHP

Preselecting Selection Lists

On a single selection list, the value of the selected <option> element is transferred to the server when submitting the form. This value can then be manually checked to prefill the form, using the selected HTML attribute, as shown in the code.

Prefilling Selection Lists
<select name="listname">
  <option value="php3"<?php
  if (isset($_POST['listname'])
    && $_POST['listname'] == 'php3') {
    echo ' selected="selected"';
  }
  ?>>PHP 3</option>
  <option value="php4"<?php
  if (isset($_POST['listname'])
    && $_POST['listname'] == 'php4') {
    echo ' selected="selected"';
  }
  ?>>PHP 4</option>
  <option value="php5"<?php
  if (isset($_POST['listname'])
    && $_POST['listname'] == 'php5') {
    echo ' selected="selected"';
  }
  ?>>PHP 5</option>
</select>

The same effect can be implemented using getFormData.inc.php; then data from the site's cookies is used, if available.

Prefilling Selection Lists
<?php
  require_once 'getFormData.inc.php';
?>
...
<select name="listname">
  <option value="php3"<?php
  if (getFormDataPOST('listname') == 'php3') {
    echo ' selected="selected"';
  }
  ?>>PHP 3</option>
  <option value="php4"<?php
  if (getFormDataPOST('listname') == 'php4') {
    echo ' selected="selected"';
  }
  ?>>PHP 4</option>
  <option value="php5"<?php
  if (getFormDataPOST('listname') == 'php5') {
    echo ' selected="selected"';
  }
  ?>>PHP 5</option>
</select>

by BrainBellupdated
Advertisement: