/    Sign up×
Community /Pin to ProfileBookmark

Dynamic Form help

I have VERY little javascript knowledge but this is what i need to do and i’m hoping someone can help me out. What i need is to create a form (used as a journal which will be emailed via php) that has 5 fields in a horizontal row. When the user loads the page they will be asked how many entries they wish to make. Once they enter a number the form will display X number of rows with those 5 fields.

Any help at all would be great

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@cgishackJan 22.2008 — Here is a sample you can hack around with.

Just past in a blank document and test...

<i>
</i>&lt;html&gt;
&lt;head&gt;

&lt;title&gt;Untitled Document&lt;/title&gt;


&lt;script&gt;


//***************************************
// Create Forms
//***************************************
function createForms(selectMenu,targetID)
{
var rowNum = parseInt(selectMenu[selectMenu.selectedIndex].value); //get user row count.
var el = document.getElementById(targetID); //get the div element to populate
var html = "";

<i> </i>//loop the total amount the user selected
<i> </i>//apply form HTML to variable
<i> </i>for (var i=0; i &lt; rowNum; i++)
<i> </i>{
<i> </i> html+= '&lt;input type="text" name=field1_'+i+'" /&gt; ';
<i> </i> html+= '&lt;input type="text" name=field2_'+i+'" /&gt; ';
<i> </i> html+= '&lt;input type="text" name=field3_'+i+'" /&gt; ';
<i> </i> html+= '&lt;input type="text" name=field4_'+i+'" /&gt; ';
<i> </i> html+= '&lt;input type="text" name=field5_'+i+'" /&gt; ';
<i> </i> html += '&lt;hr&gt;';
<i> </i>}

<i> </i>//append to div element.
<i> </i>el.innerHTML = html;
}

&lt;/script&gt;


&lt;/head&gt;

&lt;body&gt;
&lt;select id="numberOfRows" onchange="createForms(this,'formFields')"&gt;
&lt;option&gt;1&lt;/option&gt;
&lt;option&gt;2&lt;/option&gt;
&lt;option&gt;3&lt;/option&gt;
&lt;option&gt;4&lt;/option&gt;
&lt;option&gt;5&lt;/option&gt;
&lt;/select&gt;

&lt;div id="formFields"&gt;&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;


Drew
Copy linkTweet thisAlerts:
@cfgcjmauthorJan 22.2008 — Is there a way that on the page load a window (similar to an alert box) would pop up in which the user could enter the number of repetitions? as a normal text field instead of a combo box? I'll be dealing with repetitions between 10 and 30
Copy linkTweet thisAlerts:
@cgishackJan 22.2008 — var num = prompt("Type a number");
×

Success!

Help @cfgcjm 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 5.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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