My code is this:
[code]
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html lang=”EN” dir=”ltr” xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html;charset= utf8_unicode_ci”>
<title>add textboxes</title>
<script type=”text/javascript”>
function add_text() {
var form=document.getElementById(‘texts’);
var textbox=document.createElement(‘input’);
var rand=Math.random()
textbox.setAttribute (‘type’,’text’);
textbox.setAttribute (‘name’,’txt[]’);
textbox.setAttribute (‘value’,rand);
form.appendChild(textbox);
}
</script>
</head>
<body>
<?php
if (filter_has_var (INPUT_POST,”txt”)){
$txt=filter_input(INPUT_POST,”txt”);
print count($txt);
}
else
{
print <<<HERE
<input type=”button” value=”Add text field” onclick=”javascript:add_text()”>
<form id=”texts” method=”post” action=””>
<input type=”submit”>
</form>
HERE;
}
?>
</body>
</html>
In a few words it creates a button which when it is clicked it launches the add_text script. Add text script creates a text element for a form and puts a random value for the textbox. Textbox also takes a name which is of array type name=”txt[]”. Also form has the post method.
When I press e.g. 4 times the add text field button 4 text boxes are added.
But when I click the submit button I get a result of 1 instead of 4 when the
print count($txt) is executed.
Copy paste the code to see the problem.