954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

create input boxes on the fly?

Hi guys,

Bit of n00b here but do have some HTML experience.

Anyway I'm working on a small project at the moment where I require the user to enter the details of a varying number of people.

How can I create input boxes on the fly? so that the user can click 'create new person' and a new input box will show up. I plan to have an initial 10 boxes on the screen then ask the users to create any additional after that.

hope that's clear

many thanks
Kevin

jinx_uk_98
Newbie Poster
8 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Maybe you can try something like the following example:

$result = mysql_query("select * from name_table");
while ($row = mysql_fetch_array($result)) {
     echo"<label for=\"$row[id]\">$row[name]</label><input name=\"$row[id]\" type=\"text\"> </input>";
}
Rayhan Muktader
Light Poster
30 posts since Oct 2006
Reputation Points: 28
Solved Threads: 3
 

If you want to add the input boxes dynamically then you should use javascript to add them into the innerHTML of a div on your page.
The example below should do what you want, it adds new input boxes into the inner HTML of the div called "inputBoxes" whenever you click on the "Create Person" button.

<html>
<head>

<script type="text/javascript">
    var personCount=11;

    createBoxes = function(el, numberBoxes) {
        var innerHTML = "";
        var n;
        var inputBoxDiv = document.getElementById(el);
        var innerHTML = inputBoxDiv.innerHTML;

        for (n=0;n<numberBoxes;n++) {
            innerHTML=innerHTML+"<label for='person"+personCount+"'>Name:</label><input name='person"+personCount+"' type='text' /><br/>";
            personCount++;
        }
        inputBoxDiv.innerHTML = innerHTML;
    }
</script>
</head>

<body>
<form name="input" action="saveTheData.php" method="post">
    <label for="person1" >Name:</label><input type="text" name="person1"  />
    <label for="person2" >Name:</label><input type="text" name="person2"  />
    <label for="person3" >Name:</label><input type="text" name="person3"  />
    <label for="person4" >Name:</label><input type="text" name="person4"  />
    <label for="person5" >Name:</label><input type="text" name="person5"  />
    <label for="person6" >Name:</label><input type="text" name="person6"  />
    <label for="person7" >Name:</label><input type="text" name="person7"  />
    <label for="person8" >Name:</label><input type="text" name="person8"  />
    <label for="person9" >Name:</label><input type="text" name="person9"  />
    <label for="person10">Name:</label><input type="text" name="person10" />

    <div id="inputBoxes"><!-- initially empty.  The input boxes will go in here --></div>

    <button onClick="createBoxes('inputBoxes',1);">Create A New Person</button>

    <input type="submit" value="Submit">
</form>

</body>
</html>
blater
Light Poster
25 posts since Mar 2008
Reputation Points: 12
Solved Threads: 7
 

thanks blater this is perfect, I;ve managed to adapt it perfectly to my needs.

the only problem I have no have is transferring the new fields data to the mysql table I have. The rest of the form works fine but how do I reference the newly created input boxes to the sql statement know to incorporate them?


cheers

jinx_uk_98
Newbie Poster
8 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You