/    Sign up×
Community /Pin to ProfileBookmark

Help with dynamic form script

Hey all, I’m need some assistance with a dynamic form script I found at the following URL: [url]http://javascript.about.com/library/scripts/bldynoforms.htm.[/url] I’m new to javascript and don’t know much about it so I’m hoping that someone out there will graciously help me out. Anyway, I need to be able to use this script on the same page multiple times within the same form. Can someone please help me!

Thanks in advance

The link doesn’t work so here’s the code:

<script LANGUAGE=”JavaScript”>

// Browser Detection
isMac = (navigator.appVersion.indexOf(“Mac”)!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf(“MSIE 4.”)!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf(“MSIE 5.”)!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf(‘Netscape’)!=-1)?true:false;

// Body onload utility (supports multiple onload functions)
var gSafeOnload = new Array();
function SafeAddOnload(f)
{
if (IEmac && IE4) // IE 4.5 blows out on testing window.onload
{
window.onload = SafeOnload;
gSafeOnload[gSafeOnload.length] = f;
}
else if (window.onload)
{
if (window.onload != SafeOnload)
{
gSafeOnload[0] = window.onload;
window.onload = SafeOnload;
}
gSafeOnload[gSafeOnload.length] = f;
}
else
window.onload = f;
}
function SafeOnload()
{
for (var i=0;i<gSafeOnload.length;i++)
gSafeOnload[i]();
}

//
// Main Form Functions
//

// This array holds our form values when we need to regenerate the form
var gFieldValues = new Array(1);

for (var i=0;i<gFieldValues.length;i++)
gFieldValues[i]=””;

function GetFormHTML()
{
var htmlStr = ”;
htmlStr += ‘<form id=”dynoform” name=”dynoform” action=”bldynoforms.htm” method=”GET”>’;

for (var i=0;i<gFieldValues.length;i++)
htmlStr += ‘Item #’ + (i+1) + ‘ <input type=”text” name=”multifield” value=”‘ + gFieldValues[i] + ‘”><br>’;
htmlStr += ‘<input type=”button” value=”Add Item” onClick=”AddField()”>’;
htmlStr += ‘<input type=”submit” value=”Submit”>’;
htmlStr += ‘</form>’;

return htmlStr;

}

function GetFormObj()
{
var returnObj = null;

if (IE4plus)
{
returnObj = document.dynoform;
}
else if (NS4)
{
returnObj = document.formlayer.document.dynoform;
}
else if (NS6)
{
returnObj = document.getElementById(“dynoform”);
}
return returnObj;

}

function AddField()
{
// Save previously entered data here
var formObj = GetFormObj();
for (var i=0;i<gFieldValues.length;i++)
{
if (gFieldValues.length>1)
gFieldValues[i]= formObj.multifield[i].value;
else
gFieldValues[i]= formObj.multifield.value;
}

// Create the new field
gFieldValues[gFieldValues.length]=””;
UpdateForm();

}

function UpdateForm()
{
var htmlStr = GetFormHTML();
if (IE4plus)
{
document.all.formlayer.innerHTML = htmlStr;
}
else if (NS4)
{
document.formlayer.document.open();
document.formlayer.document.write(htmlStr);
document.formlayer.document.close();
}
else if (NS6)
{
document.getElementById(“formlayer”).innerHTML = htmlStr;
}
}

function IncludeForm()
{
var htmlStr = GetFormHTML();

if (IE4plus || NS6)
{
document.write(‘<DIV ID=formlayer name=formlayer STYLE=”position:relative; WIDTH=400px; HEIGHT=50px”>’ + htmlStr + ‘</DIV>’);
}
else if (NS4)
{
// Because NS needs floating layers, we need a placeholder graphic to force anything
// below the layer content to leave whitespace for the layer. The position of this
// graphic is also used in determining the position of the layer.
document.write(‘<img name=”formlocation” border=”0″ width=”400″ height=”200″ src=”images/spacer.gif”>’);

}

}

//
// Netscape 4.x Ineptness
//
function HandleOnload()
{
if (NS4)
{
var width = document.formlocation.width;
var height = document.formlocation.height;

nL=new Layer(width);
nL.name = “formlayer”;
nL.left=document.formlocation.x;
nL.top=document.formlocation.y;
nL.bgColor = “white”;
nL.clip.width=width;
nL.clip.height=height;
nL.document.open();
nL.document.write(GetFormHTML());
nL.document.close();
nL.visibility = ‘show’;

document.formlayer = nL;

}

}

function HandleResize()
{
location.reload();
return false;
}

if (NS4)
{
SafeAddOnload(HandleOnload);
window.captureEvents(Event.RESIZE);
window.onresize = HandleResize;
}

</script>

<P><u>Grocery List</u></P>
<script LANGUAGE=”JavaScript”>
IncludeForm();
</script>

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @JohnnyAnonymous spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...