...

View Full Version : Creating text element for a form with createElement makes text element not work



pananag
11-16-2011, 04:07 PM
My code is this:

<!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.

venegal
11-16-2011, 04:38 PM
You can't filter an array like that.

This is a PHP issue, not Javascript, so I'm moving the thread to the PHP forum.

Dormilich
11-17-2011, 08:02 AM
you will at least require the flag FILTER_REQUIRE_ARRAY. this is either passed as 4th parameter or under the "flags" key of the additional options array.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum