Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

PHP Select Form Helper

// takes an array of values and a value to match, and outputs formatted <option>s with the <option> matching $match selected
// must be manually wrapped in <select></select to allow for maximum flexibility

function selectHelper($values, $match)
{
  $keys = array_keys($values);
  $i = 0;
	
  foreach($values as $option)
  {
    $selected = null;
		
    if($match == $keys[$i])
      $selected = " selected";
			
    echo "	<option value=\"".$keys[$i]."\"$selected>".$option."</option>\n";
		
    $i++;
  }
}

//sample usage:
$values = array(
  "lb" => "Pounds",
  "ea" => "Each",
  "oz" => "Ounces");
				
  selectHelper($values, $product->unit);

No duplicate forms in MDI Parent

//This will ensure that no duplicate copies of a child form is created in an //MDI_Parent

            //"Display" is the form name
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm.GetType() == typeof(Display))
                {
                    childForm.Focus();
                    return;
                }
            }
            Display frmDisplay = new Display();
            frmDisplay.MdiParent = this;
            frmDisplay.Show();

Close all forms in Access

Sometimes, it's necessary to close all forms in a single procedure:

Function CloseAllForms()

'It will close all forms before opening the new form (if required)

Dim obj As Object
Dim strName As String

For Each obj In Application.CurrentProject.AllForms
DoCmd.Close acForm, obj.name, acSaveYes
Next obj

End Function


Close all forms - except one particular screen:


Function CloseAllForms()


Dim obj As Object
Dim strName As String

For Each obj In Application.CurrentProject.AllForms

If obj.Name <> "Your Form" Then 

DoCmd.Close acForm, obj.name, acSaveYes

End if 
Next obj

End Function

Extracting the values of all forms on the page

This is a simple piece of javascript intended to be run from Firebug or as a bookmarklet which extracts all the name : value pairs from forms on the page and pops up a new window listing them.

It's not very well written, and doesn't yet handle any non input form elements, but it will do for now. :-)

var displayWindow = window.open();

function showFormValues(form ) { 
    displayWindow.document.write('Form:');
    displayWindow.document.write(form.name);
    displayWindow.document.write('<br>');

    var formElements = form.getElementsByTagName('input');

    for (var i = 0; i < formElements.length; i++){
        var element = formElements[i];
        
        displayWindow.document.write(element.name + ' :  ' + element.value + ' <br>');}} 

Array.forEach(document.forms, showFormValues);


Is Form Dirty?

Determines if a form is dirty - i.e. if any of its elements have had their state modified.

/**
 * Determines if a form is dirty by comparing the current value of each element
 * with its default value.
 *
 * @param {Form} form the form to be checked.
 * @return {Boolean} true if the form is dirty, false otherwise.
 */
function formIsDirty(form)
{
    for (var i = 0; i < form.elements.length; i++)
    {
        var element = form.elements[i];
        var type = element.type;
        if (type == "checkbox" || type == "radio")
        {
            if (element.checked != element.defaultChecked)
            {
                return true;
            }
        }
        else if (type == "hidden" || type == "password" || type == "text" ||
                 type == "textarea")
        {
            if (element.value != element.defaultValue)
            {
                return true;
            }
        }
        else if (type == "select-one" || type == "select-multiple")
        {
            for (var j = 0; j < element.options.length; j++)
            {
                if (element.options[j].selected !=
                    element.options[j].defaultSelected)
                {
                    return true;
                }
            }
        }
    }
    return false;
}

Check / Uncheck all checkboxes in a pseudo group

This bit of javascript will check and uncheck all checkboxes in a group of checkboxes. The checkboxes are grouped by naming all the checkboxes by the same name.

Javascript Code:
function checkUncheckAll(checkAllState, cbGroup)
{
	for (i = 0; i < cbGroup.length; i++)
	{
		cbGroup[i].checked = checkAllState.checked;
	}
}


HTML Code:
<input type=checkbox name=checkall onclick="checkUncheckAll(this, grp1);">
<input type=checkbox name=grp1 id=bx1>
<input type=checkbox name=grp1 id=bx2>
<input type=checkbox name=grp1 id=bx3>
<input type=checkbox name=grp1 id=bx4>

javascript - send multi-forms

<script type="text/javascript">
function sendcopy(el) {
	el.action = '/cgi-bin/action2.pl'
	el.submit()
	el.action = '/cgi-bin/action1.pl'
	el.submit()
}
</script>



//<form id="myform" method="post" action="" onsubmit="sendcopy(this)">
//-------------------------------------------------------------------


//However, this seems NOT to work with Opera 7 and Netscape 4. They only send the form to action1. Mozilla and IE are behaving as I hoped they would, and are calling both.
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS