/    Sign up×
Community /Pin to ProfileBookmark

Text element in form created with createElement can’t pass post variable

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>
[/code]

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.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyNov 17.2011 — Please use the code tag when pasting your code please.

Now, a couple of things. One is that you don't need the 'javascript:' text included in the onclick event. This is really only used for links(as you need 'javascript: ;' to execute javascript commands/code in the addressbar, which is where links submit their href value to).

Next, if I had to take a guess it would be to name the text field simply 'txt' rather than attempting to make it an array by calling it 'txt[]'. When a field in a form has the same name as another field they are treated as an array naturally without the use or need of appending [] to the end. This is seen and done all the time with radio inputs for instance. So all of the text inputs can simply have the same name of 'txt' and when you call the object in your form called 'txt' it will return as an array containing all of the values(properties and etc.).
Copy linkTweet thisAlerts:
@pananagauthorNov 17.2011 — 

Next, if I had to take a guess it would be to name the text field simply 'txt' rather than attempting to make it an array by calling it 'txt[]'. When a field in a form has the same name as another field they are treated as an array naturally without the use or need of appending [] to the end. This is seen and done all the time with radio inputs for instance. So all of the text inputs can simply have the same name of 'txt' and when you call the object in your form called 'txt' it will return as an array containing all of the values(properties and etc.).[/QUOTE]


Thanks. I'll try that. hope it works.
Copy linkTweet thisAlerts:
@pananagauthorNov 17.2011 — 

Next, if I had to take a guess it would be to name the text field simply 'txt' rather than attempting to make it an array by calling it 'txt[]'. When a field in a form has the same name as another field they are treated as an array naturally without the use or need of appending [] to the end. This is seen and done all the time with radio inputs for instance. So all of the text inputs can simply have the same name of 'txt' and when you call the object in your form called 'txt' it will return as an array containing all of the values(properties and etc.).[/QUOTE]


Tried. It didn't work. Any other ideas?
Copy linkTweet thisAlerts:
@3NexNov 17.2011 — The post goes fine and your textboxes are sent normally. You will see that if you run the code as:
[CODE]echo count($_POST['txt'])[/CODE]
it will return the correct number.


Now, what filter_has_var and all that other mumbo jumbo do, i have no freakin idea. But your problem is in the PHP part of the program, not the javascript.
Copy linkTweet thisAlerts:
@pananagauthorNov 17.2011 — The post goes fine and your textboxes are sent normally. You will see that if you run the code as:
[CODE]echo count($_POST['txt'])[/CODE]
it will return the correct number.


Now, what filter_has_var and all that other mumbo jumbo do, i have no freakin idea. But your problem is in the PHP part of the program, not the javascript.[/QUOTE]


It works!

Are there any complications with replacing filter_input with $_POST?
Copy linkTweet thisAlerts:
@3NexNov 18.2011 — No idea.
Copy linkTweet thisAlerts:
@jalarieNov 19.2011 — You don't need the 'javascript:' text included in the onclick event.[/QUOTE]
You do if you also use VBS routines on the same page. I had a perfecly-working page with lots of JS and tried to add some already-tested VBS routines and everything suddenly stopped working. It took me FOREVER to find that I needed to add "javascript:" at the beginning of all JS items.
×

Success!

Help @pananag 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 6.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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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