Приглашаем посетить
Путешествия (otpusk-info.ru)

Preselecting Multiple Selection Lists

Previous
Table of Contents
Next

Preselecting Multiple Selection Lists


if (isset($_POST['multilistname']) && in_array('php4',
Preselecting Multiple Selection Lists $_POST['multilistname'])) {
   echo ' selected="selected"';
}


When it comes to prefilling form elements, multiple selection lists are the most difficult ones to implement. This is because in $_GET or $_POST, you have an array of chosen options; so you cannot just compare strings, but you have to search for the specified value in the array. Luckily, PHP offers something suitable in the form of the in_array() function. So, the effort required is not much more than with the other form elements: If the current value is in $_GET/$_POST, print out the selected attribute.

Prefilling Multiple Selection Lists (select-multiple.php; excerpt)
<select name="multilistname[]" multiple="multiple"
   size="3">
  <option value="php3"<?php
  if (isset($_POST['multilistname']) &&
     in_array('php3', $_POST['multilistname'])) {
    echo ' selected="selected"';
  }
  ?>>PHP 3</option>
  <option value="php4"<?php
  if (isset($_POST['multilistname']) &&
    in_array('php4', $_POST['multilistname'])) {
    echo ' selected="selected"';
  }
  ?>>PHP 4</option>
  <option value="php5"<?php
  if (isset($_POST['multilistname']) &&
    in_array('php5', $_POST['multilistname'])) {
    echo ' selected="selected"';
  }
  ?>>PHP 5</option>
</select>

However, the HTML form must be specially prepared to allow PHP to access the data from the multiple selection list: The value of the name attribute has to end with [], hinting to PHP that it should expect an array of values, not just a string value. Accessing the list data, however, can still be done using $_GET['listname']/$_POST['listname'] and not $_GET['listname[]']/$_POST['listname[]'], as shown in the preceding code.

If you want to prefill the list with data from the cookie, you just have to use the well-known file getFormData.inc.php from the previous phrases.

It contains two additional functions that return an array instead of a string.

Retrieving Form Data from a Cookie (getFormData.inc.php; excerpt)
function getFormDataArrayGET($name) {
  if (isset($_GET[$name])) {
    return $_GET[$name];
  } else {
    $cookiedata = getCookieData();
    if ($cookiedata != null && 
      isset($cookiedata[$name])) {
      return $cookiedata[$name];
    }
  }
  return array();
}
function getFormDataArrayPOST($name) {
  if (isset($_POST[$name])) {
    return $_POST[$name];
  } else {
    $cookiedata = getCookieData();
    if ($cookiedata != null && 
      isset($cookiedata[$name])) {
      return $cookiedata[$name];
    }
  }
  return array();
}

These functions return an array for multiple lists that you can use as you did in select-multiple.php.

Prefilling Multiple Selection Lists (select-multiple-cookie.php; excerpt)
<?php
  require_once 'getFormData.inc.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
   Transitional//EN" "http://www.w3.org/TR/xhtml1/
   DTD/
xhtml1-transitional.dtd">
<html>
<head>
  <title>Forms</title>
</head>
<body>
  <form method="post" action="
<?php echo htmlspecialchars($_SERVER['PHP_SELF']);
   ?>">
    <select name="multilistname[]"
    multiple="multiple" size="3">
      <option value="php3"<?php
  if (in_array('php3', getFormDataArrayPOST
   ('multilistname'))) {
    echo ' selected="selected"';
  }
      ?>>PHP 3</option>
      <option value="php4"<?php
  if (in_array('php4', getFormDataArrayPOST
   ('multilistname'))) {
    echo ' selected="selected"';
  }
      ?>>PHP 4</option>
      <option value="php5"<?php
  if (in_array('php5', getFormDataArrayPOST
   ('multilistname'))) {
    echo ' selected="selected"';
  }
      ?>>PHP 5</option>
    </select><br />
    <input type="submit" />
  </form>
</body>
</html>


Previous
Table of Contents
Next