Forum Moderators: open
var newrow = document.createElement('tr');var td1 = document.createElement('td');
td1.setAttribute('valign', 'top');
var input1 = document.createElement('input');
input1.type = 'text';
input1.setAttribute('size', 5);
input1.name = 'name2';
input1.value = '';td1.appendChild(input1);
newrow.appendChild(td1);
Thanks for any input or suggestions.
Thanks for the suggestion. Unfortunately in this case, that probably isn't the best solution, because I need the form elements to be created dynamically as needed. Basically I have some set form fields, and when you click a link, it adds duplicates of that field each time, so there is no way to tell how many times the user might need to click the link.
What I might do if I can't get this figured out soon is just eschew the javascript and instead just make the link send the page back to the server to generate the new fields. I just wanted the javascript solution because it is more elegant and avoids all the page refreshing.
<?php
if (array_key_exists('name2', $_POST)) {
print_r($_POST);
exit;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
<title>Untitled</title>
</head>
<body>
<div>
<form method="POST">
<table>
<tbody id="formRows">
<tr>
<td><input type="text" name="foo"></td>
</tr>
</tbody>
</table>
<input type="submit" value="Submit">
</form>
</div>
<!-- Scripts -->
<script type="text/javascript">
var formRows = document.getElementById('formRows');
var newrow = document.createElement('tr');
var td1 = document.createElement('td');
td1.setAttribute('valign', 'top');
var input1 = document.createElement('input');
input1.type = 'text';
input1.setAttribute('size', 5);
input1.name = 'name2';
input1.value = '';
td1.appendChild(input1);
newrow.appendChild(td1);
formRows.appendChild(newrow);
</script>
</body>
</html>
In that example I'm only appending a single input, but it shouldn't matter if you append more. When I submit the form, I saw that the data was correctly posted.