? Earlier 4 items total Later ?

On this page:?

Selecting and checking items from HTML menus

This code allows you to pass in an HTML menu (select, radio, or checkbox) along with a single choice or an array of choices to be selected. The choice(s) should be contained literally in the value attribute.

This is handy for making persistent forms without polluting the HTML with PHP logic. For example, you could initialize all your menus to have nothing selected and then pass in GET or POST variables in case the form was not accepted, easily making form selections persistent.

/**
 * For select menus.
 */
function select_from_menu($menu,$choices,$deselect=true) {
  if ($deselect) $menu = preg_replace('/ selected(="selected")?/','',$menu);
  if(!is_array($choices)) {
    $menu = preg_replace('/(value="'.preg_quote($choices).'")/',"$1 selected=\"selected\"",$menu);
  } else foreach($choices as $value) {
    $menu = preg_replace('/(value="'.preg_quote($value).'")/',"$1 selected=\"selected\"",$menu);
  }
  
  return $menu;
}

/**
 * For radio buttons and checkboxes.
 */
function check_from_menu($menu,$choices,$deselect=true) {
  if ($deselect) $menu = preg_replace('/ checked(="checked")?/','',$menu);
  if(!is_array($choices)) {
    $menu = preg_replace('/(value="'.preg_quote($choices).'")/',"$1 checked=\"checked\"",$menu);
  } else foreach($choices as $value) {
    $menu = preg_replace('/(value="'.preg_quote($value).'")/',"$1 checked=\"checked\"",$menu);
  }

  return $menu;
}

Clear form with Javascript

Purpose: Clear all text fields in a form

Other important features:
- Resulting page must be valid XHTML 1.1
- JS must work in Firefox 1.0 and MS Internet Explorer 6.0
- JS must never generate any errors, even non-fatal ones, in the Firefox Javascript Console
- JS should only clear text input (no hidden fields or visual controls)
- Code must be readable and properly indented

Looking around the web, this was not easy to find. After a few hours of searches, and some back'n'forth at Webmaster World, I got a working version.

In XHTML header:


In html/form/div


In clear-form-input.js:

function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

        if (document.getElementsByTagName)
        {
                elements = form.getElementsByTagName('input');
                for( i=0, elm; elm=elements.item(i++); )
                {
                        if (elm.getAttribute('type') == "text")
                        {
                                elm.value = '';
                        }
                }
        }

        // Actually looking through more elements here
        // but the result is the same.
        else
        {
                elements = form.elements;
                for( i=0, elm; elm=elements[i++]; )
                {
                        if (elm.type == "text")
                        {
                                elm.value ='';
                        }
                }
        }
}
        
function addEvent(elm, strEvent, fnHandler)
{
        return ( elm.addEventListener
        ? elm.addEventListener( strEvent, fnHandler, false)
        : elm.attachEvent( 'on'+strEvent, fnHandler)
        );
}

mod_rewrite rules to serve application/xhtml+xml

The following mod_rewrite rules will serve HTML files as application/xhtml+xml to supporting browsers.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_ACCEPT} application/xhtml\+xml
RewriteCond %{HTTP_ACCEPT} !application/xhtml\+xml\s*;\s*q=0
RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{THE_REQUEST} HTTP/1\.1
RewriteRule .* - [T=application/xhtml+xml]


Source: Mark Pilgrim, The Road to XHTML 2.0: MIME Types.

Serve XHTML 1.1 Properly

Place this in a .htaccess file or your Apache configuration. Note that this will redirect noncompliant users to a chosen URL. All XHTML 1.1 pages must be saved with the .xhtml extension.

AddType application/xhtml+xml .xhtml


    SecFilterEngine On
    SecFilterScanOutput On
    SecFilterOutputMimeTypes "application/xhtml+xml"
    SecFilterDefaultAction "pass"
    SecFilterSelective "HTTP_ACCEPT" "!application/xhtml\+xml" "redirect:http://www.webstandards.org/upgrade/"

? Earlier 4 items total Later ?