Learn HTML Code Tutorial Reference Guide

Submit Image Changes When Data is Ready

In this variation on the Rollover Submit Image, the image changes not when the mouse is over it but when the user has typed something into the fields. The image changes to signal that the form is ready for submitting:

To create this type of image submit, first copy this script exactly as is into the <HEAD> section of your page:

<SCRIPT TYPE="text/javascript">
<!--
// Script by Idocs Inc, http://www.idocs.com
// Distribute freely, but keep this notice in place
var swrlist = new Array();

function swapwhenready(ready, notready)
{
this.ready=ready;
this.notready=notready;
this.alt="submit query";
this.name=document.forms[document.forms.length-1].name;
this.setfields     = swr_setfields;
this.checkfields   = swr_checkfields;
this.write         = swr_write;
this.submitswap    = swr_submitswap;
swrlist[this.name] = this;
}

function swr_setfields()
{
this.fields = new Object();
for (var i=0; i < arguments.length; i++)
   this.fields[arguments[i]]=true;
}

function swr_checkfields()
{
var myForm=document.forms[this.name];

for (var i=0; i < myForm.elements.length; i++)
   {
   if (
      (myForm.elements[i].value.length == 0) && 
      (this.fields ? myForm.elements[i].name && 
      this.fields[myForm.elements[i].name] : true)
      )
      return false;
   }
return true;
}

function swr_write()
{
if (
   document.images && 
   (document.forms.length > 0)
   )
   {
   this.readyImage = new Image();
   this.readyImage.src = this.ready;
   this.notReadyImage = new Image();
   this.notReadyImage.src = this.notready;
   this.imagename = this.name + '_swrimage';
   this.linkname = this.name + '_swrlink';

   document.write(
      '<A ' + 
      ' HREF="javascript:submitit(\'' + this.name + '\');void(0);"' + 
      ' NAME="' + this.linkname + '"' + 
      '><IMG SRC="' + this.notready + '"' + 
      ' ALT="' + this.alt + '"' + 
      ' NAME="' + this.imagename + '"' + 
      ' BORDER=0');
   if (this.height)document.write(' HEIGHT=' + this.height);
   if (this.width)document.write(' WIDTH=' + this.width);
   if (this.otheratts)document.write(" " + this.otheratts);
   document.write('></A>');
   }
else
   {
   document.write(
      '<INPUT TYPE=IMAGE SRC="' + this.ready + '"' + 
      ' ALT="' + this.alt + '"');
   if (this.otheratts)
      document.write(" " + this.otheratts);
   document.write('>');
   }
this.submitswap();
}

function submitswap(myfield)
   {swrlist[myfield.form.name].submitswap()}
function swr_submitswap()
{
if (this.checkfields())
   document.images[this.imagename].src = this.readyImage.src;
else
   document.images[this.imagename].src = this.notReadyImage.src;
}

function submitcheck(myform)
   {return swrlist[myform.name].checkfields()}

function submitit(name)
{
if (swrlist[name].checkfields())
   document.forms[name].submit();
}

//-->
</SCRIPT>

Now, suppose our form has one field we want filled in before the form is ready. Let's say the form is named "myform" and the field to check is named "searchstring". We create the form like this:

<FORM 
   ACTION="../cgi-bin/mycgi.pl"
   NAME="myform" 
   onSubmit="return submitcheck(this)">

<INPUT 
   NAME="searchstring" SIZE=10
   onKeyUp="submitswap(this)"
   >

<SCRIPT TYPE="text/javascript">
<!--
var swapper = new swapwhenready("search.ready.gif","search.notready.gif");
swapper.write();
//-->
</SCRIPT>

<NOSCRIPT><INPUT TYPE=SUBMIT></NOSCRIPT>

</FORM>

Search!
"search.notready.gif"
Search!
"search.ready.gif"
 
HOSTING / DESIGN
MAKE MONEY

Home
  |   Tutorials   |   Forum   |   Quick List   |   About
Copyright ©1997-2002 Idocs and ©2002-2007 HTML Code Tutorial