?
Solved

Dynamic form fields added with appendChild or innerHTML do not POST on submit

Posted on 2007-08-07
7
Medium Priority
?
287 Views
Last Modified: 2013-11-05
Hi there I have the following code which you can paste directly into a file and test.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<link href="styles/screen.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="js/Jscript.js"></script>

<script type="text/javascript">

             
        function formvalidation(thisform)
        {
        with (thisform)
        {
       
        var where_to= confirm("All Form Changes You've Made Will Now Be Saved.");
        if (where_to== true)
        {
            thisform.action = 'PPForm_3.asp?Action=Next';                  
            thisform.project.submit();
        }          
        }
        }
       
       function addEvent(eventtype)
        {

        var ni = document.getElementById("myDiv"+eventtype);
        var numi = document.getElementById('theValue');
        var num = (document.getElementById("theValue").value -1)+ 2;
        numi.value = num;
        var divIdName = "my"+num+"Div";
        var newdiv = document.createElement('div');
        newdiv.setAttribute("id",divIdName);  
        if (eventtype == 'link')
        {
        newdiv.innerHTML = "<input type=\"file\" name="+eventtype+num+" /> <a href=\"javascript:;\" onclick=\"removeEvent(\'"+divIdName+"\',\'" + eventtype + "\')\">Remove";
        }
        else
        {
        newdiv.innerHTML = "<input type=\"text\" name="+eventtype+num+" /> <a href=\"javascript:;\" onclick=\"removeEvent(\'"+divIdName+"\',\'" + eventtype + "\')\">Remove";
        }
       
        ni.appendChild(newdiv);
        }

        function removeEvent(divNum,eventtype)
        {
        var d = document.getElementById("myDiv"+eventtype);
        var olddiv = document.getElementById(divNum);
        d.removeChild(olddiv);
        }
           
</script>

</head>

<form
    name="project"
    method="post"        
    class="cmxform"
    onsubmit="return formvalidation(this);"
    enctype="multipart/form-data" >
    <input type="hidden" name="ID" value="<%=Request.QueryString("ID") %>">  
<!-- #include file="includes/header.asp" -->  
  <tr>
    <td height="943"></td>
    <td valign="top">
     <table width="100%" border="0" cellpadding="2" cellspacing="3">    
      <tr>
        <td width="16" height="14"></td>
          <td width="786"></td>
      </tr>
      <tr>
        <td height="444"></td>
        <td valign="top">
           
            <table width="544" border="0" cellpadding="2" cellspacing="3">
              <tr>
                <td>Related Links</td>
                <td colspan="2">
                    <label>
                        <input type="hidden" value="0" id="theValue" />
                <p><a href="javascript:;" onclick="addEvent('link');">+ Add Some Related links +</a></p>
                <div id="myDivlink"> </div>
                    </label>
                </td>
              </tr>  
               <tr>
                <td>Related URL's</td>
                <td colspan="2">
                    <label>
                        <input type="hidden" value="0" id="Hidden1" />
                <p><a href="javascript:;" onclick="addEvent('url');">+ Add Some Related URL's +</a></p>
                <div id="myDivurl"> </div>
                    </label>
                </td>
              </tr>  
             
             
             
              <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td align="right"><input type='submit' value='Next >>'></td>
               
              </tr>
            </table>

            </td>
        </tr>
      <tr>
        <td height="485"></td>
        <td>&nbsp;</td>
      </tr>
      <!--DWLayoutTable-->
        </table>
        </td>
    <td></td>
  </tr>
  </form>
</table>

</body>
</html>

I can seem to get the form variables to post anything. The form submit is multipart enctype as the dynamic elements are "file" types.

Can Anyone Help?
0
Comment
Question by:MayoorPatel
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19644151
We need
js/Jscript.js
too
0
 
LVL 1

Author Comment

by:MayoorPatel
ID: 19644175
here you go!

// JScript File

function openpopup(popurl){
var winpops=window.open(popurl,"","width=500,height=150,resizable")
}

function getClient() {
if (window.frames.length==0) return ""; // no iframes here
var ifr = window.frames['ifrClient'];
if (!ifr) return ""; // no iframe called ifrClient here      
var sel = ifr.document.getElementsByName('ClientID')[0]
return sel.options[sel.selectedIndex].value
 
}

function getContact() {
if (window.frames.length==0) return ""; // no iframes here
var ifr = window.frames['ifrContact'];
if (!ifr) return ""; // no iframe called ifrClient here      
var sel = ifr.document.getElementsByName('ContactID')[0]
return sel.options[sel.selectedIndex].value
 
}

function getDesignPhase() {
if (window.frames.length==0) return ""; // no iframes here
var ifr = window.frames['ifrDesign'];
if (!ifr) return ""; // no iframe called ifrClient here      
var sel = ifr.document.getElementsByName('DesignPhaseID')[0]
return sel.options[sel.selectedIndex].value
}

function valuevalidation(entered, min, max, alertbox, datatype)
{      
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
  {smalldatatype=datatype.toLowerCase();
   if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
  }
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function digitvalidation(entered, min, max, alertbox, datatype)
{      
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
  {smalldatatype=datatype.toLowerCase();
   if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value); if (value.indexOf(".")!=-1) {checkvalue=checkvalue+1}};
  }
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max && value.length>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function emptyvalidation(entered, alertbox)
{
with (entered)  
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 19645847
Hi,

  Taking a break from unpacking in new home, so really don't have much time...but this:

   with (this.form)
........
      thisform.project.submit();
        }          

seems somewhat strange to me.   I'm presuming the 'thisform' is the form object.  If that is the case, what is 'project'????  If the form name, it's unneeded.

(oh well, back to the boxes..... :)
 
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1000 total points
ID: 19648088
Well spotted

1.  thisform.submit();
2. do NOT submit in an onSubmit handler so the line should be removed completely
0
 
LVL 15

Assisted Solution

by:VincentPuglia
VincentPuglia earned 1000 total points
ID: 19650042
re #2:  or, change the submit button to a normal button, use onclick to do the validation, & keep the 'thisform.submit()' statement

Also, I assume you still have to include your validation....because the function really does nothing other than set the action & submit.


/  /the 'where_to" variable is superfluous in the code you posted
 function formvalidation(thisform)   {
      if ( confirm("All Form Changes You've Made Will Now Be Saved.") )
     {      
           thisform.action = 'PPForm_3.asp?Action=Next';                  
           thisform..submit();
     }          
}

The following link demonstrates how you could validate fields before submission; it was written years ago, but still contains many valid points.
 
  http://members.aol.com/grassblad/html/validForm.html     

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question