SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Dynamic form

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic form

    I have been looking at the dynamic form script on (http://javascript.about.com/library/...ldynoforms.htm). It's good and nearly solves my problem but I want to add to it so that there are five new different form input boxes each time I press 'Add Item'.

    Can anyone help me out? Also, I am stumped as to how to read values from an array form element using php? I pass the values of this script into php which uses $_POST to extract the form values but all I get is 'multifield' = 'whatever the last value entered was' - where are the other values?

    I tried changing this line in the script:
    Code:
    htmlStr += 'Item #' + (i+1) + ' <input type="text" name="multifield" value="' + gFieldValues[i] + '"><br>';
    to
    Code:
    htmlStr += 'Item #' + (i+1) + ' <input type="text" name="multifield[]" value="' + gFieldValues[i] + '"><br>';
    (added [] to the name), but then the script doesn't work at all.

  2. #2
    SitePoint Member jjwxg's Avatar
    Join Date
    Nov 2003
    Location
    China
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    window.onload=function test(){
    mul=document.getElementsByName("multifield[]");
    for(i=0;i<mul.length;i++)
    alert(mul[i].value);
    }
    //-->
    </SCRIPT>
    <INPUT TYPE="text" NAME="multifield[]" value="aaa">
    <INPUT TYPE="text" NAME="multifield[]" value="bbb">
    <INPUT TYPE="text" NAME="multifield[]" value="ccc">

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm sorry, but I don't understand that. Is it supposed to help with adding more than one input field or is it meant to help read them back?

  4. #4
    SitePoint Zealot pacman2504's Avatar
    Join Date
    Oct 2003
    Location
    North Wales
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    To display extra form fields, amend the //Create the new field section of the code to this:
    Code:
    // Create the new field
    	gFieldValues[gFieldValues.length]="";
    	gFieldValues[gFieldValues.length]="";
    	gFieldValues[gFieldValues.length]="";
    	gFieldValues[gFieldValues.length]="";
    	UpdateForm();
    }
    
    function UpdateForm()
    {
    	var htmlStr = GetFormHTML();
    	if (IE4plus)
    	{
    		document.all.formlayer.innerHTML = htmlStr;
    	}
    	else if (NS4)
    	{
    		document.formlayer.document.open();
    		document.formlayer.document.write(htmlStr);
    		document.formlayer.document.close();
    	}
    	else if (NS6)
    	{	
    		document.getElementById("formlayer").innerHTML = htmlStr;
    	}
    }
    Not sure about the other problem, but I'd like to know how to do this too.

    Hope this helps.
    In the land of the witless, the half-wit is king.
    Business and liability insurance quotes

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks But then the input fields end up having the same name, don't they? What if I want the input fields to have different names? Like, each time the button is pressed, a field with the name "Address" and a field with the name "City" is added.


Bookmarks

Posting Permissions

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