/    Sign up×
Community /Pin to ProfileBookmark

Posting values with dynamically created elements

HI Guys

I am using the following code that creates html form elements dynamically but there is one problem.

[CODE]
<html>
<head>
<title>Adding and Removing Text Boxes Dynamically</title>
<SCRIPT language=”javascript”>
var intTextBox=0;

//FUNCTION TO ADD TEXT BOX ELEMENT
function addElement()
{
intTextBox = intTextBox + 1;
var contentID = document.getElementById(‘content’);
var newTBDiv = document.createElement(‘div’);
var submit = document.getElementById(‘submit’).style.visibility=”visible”;
newTBDiv.setAttribute(‘id’,’strText’+intTextBox);
newTBDiv.innerHTML = “Serial numbers “+intTextBox+ “<input type=’checkbox’ id=’cb” + intTextBox + “‘ name=’cb[]’/>” + “: <input type=’text’ id=’nm” + intTextBox + “‘ name=’nm[]’/> — <input type=’text’ id=’vl” + intTextBox + “‘ name=’vl[]’/><br/><br/>”;
contentID.appendChild(newTBDiv);
}

//FUNCTION TO REMOVE TEXT BOX ELEMENT
function removeElement()
{
if(intTextBox != 0)
{
var contentID = document.getElementById(‘content’);
contentID.removeChild(document.getElementById(‘strText’+intTextBox));
intTextBox = intTextBox-1;

if(intTextBox < 1) {
document.getElementById(‘submit’).style.visibility=”hidden”;

}

}
}
</SCRIPT>
</head>
<body>
<p>Demo of Adding and Removing Text Box Dynamically using JavaScript</p>

<form action=’post.php’ method=’post’>
<div id=”content”></div>
<div id=”submit” style=”visibility: hidden;”>
<input type=’submit’ value=’submit’>

</div>
</form>
<p><a href=”javascript:addElement();” >Add</a> <a href=”javascript:removeElement();” >Remove</a></p>
</body>
</html>
[/CODE]

And I am getting an output like the following.

[CODE]
Array
(
[cb] => Array
(
[0] => on
[1] => on
)

[nm] => Array
(
[0] => a
[1] => b
)

[vl] => Array
(
[0] => 1
[1] => 2
[2] => 3
)

)
[/CODE]

Now, in the above output how do I know pragmatically which checkbox values were checked and which textbox were filledup?

If there is anyway we can use names of the elements instead of indexed arrays? For example

[CODE]Array
(
[t_x] => 1
[t_y] => 2
[t_z] => 3
)
[/CODE]

THanks[/QUOTE]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @phantom007 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.28,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

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