CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    17

    JS-generated text boxes is not sent on submit!

    Hello!

    I have no example right now, but I earlier this day created a program which generated textboxes to appear inside the form which I later submitted them with. But after submit, the boxes generated with JavaScript and added through the DOM wasn't sent, only those which where created when the document loaded (written directly in the html-document) where.

    Why does this problem exist and how can I solve it? I do not wich to use the add-the-variables-to-the-adress method. Do I have to add them by changing in the html-code rather than creating DOM objects? Moreover I'm running IE 7.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: JS-generated text boxes is not sent on submit!

    As long as elements are created properly, they can be added to a form any time after document load. Are you sure you created them properly and attached them to the form?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jul 2007
    Posts
    17

    Re: JS-generated text boxes is not sent on submit!

    Well, how's properly? I created them and attached them to an element which lies inside the form and they all pop up on the screen so I can fill them in. What more have gone wrong?

  4. #4
    Join Date
    Jul 2007
    Location
    Sweden
    Posts
    331

    Re: JS-generated text boxes is not sent on submit!

    Try the following:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>JavaScript - Dynamic form fields</title>
    <script type="text/javascript">
    var i = 1;
    function newField(before) {
    	var p = document.createElement("p");
    
    	var id = "field-x-" + i;
    
    	var input = document.createElement("input");
    	input.id = id;
    	input.name = "x-" + i;
    	input.type = "text";
    
    	var label = document.createElement("label");
    	label["for"] = id;
    
    	var text = document.createTextNode("Extra #" + i);
    
    	i++;
    
    	label.appendChild(text);
    	p.appendChild(label);
    	p.appendChild(input);
    	before.parentNode.insertBefore(p, before);
    }
    </script>
    <style type="text/css">
    label {
    	display: block;
    	float: left;
    	font-weight: bold;
    	width: 5em;
    }
    </style>
    </head>
    <body>
    <form action="js.html" method="get">
    	<p>
    		<label for="field-name">Name</label>
    		<input id="field-name" name="name" type="text" />
    	</p>
    	<p>
    		<input onclick="newField(this.parentNode)" type="button" value="New field" />
    		<input type="submit" value="Submit form" />
    	</p>
    </form>
    </body>
    </html>
    One possible problem with your code could be that you forgot the name property.
    Last edited by andreasblixt; August 10th, 2007 at 02:30 AM. Reason: Fixed ridiculous IE bug (label.for = id; - it thinks there is a for loop...)

  5. #5
    Join Date
    Jul 2007
    Posts
    17

    Re: JS-generated text boxes is not sent on submit!

    Quote Originally Posted by andreasblixt
    One possible problem with your code could be that you forgot the name property.
    I believe you hit the head on the nail! I'm not sure though, I have to test it first nut the name attribute definitely wasn't set. I thought I had done that... Thanks, anyway!

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: JS-generated text boxes is not sent on submit!

    Like I said, so long as you create it properly, it works. Next time, post some code and it would help.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured