Add new textfields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
seahorse123
Forum Newbie
Posts: 8
Joined: Tue Sep 20, 2005 9:06 pm

Add new textfields

Post by seahorse123 »

I am just wondering is there a way in php to add new textfield when I click a "Add" button, for example on the form, I am not sure how many items user will enter, so I want to create a textfield with a button "Add", when user click "Add" button it will popup a new textfield.
I know is possible in javascript, however the form has a lot of data, I don't want to use "GET" method to have it listed in the url, just want to know is it possible to implement in php? thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that'd be done through Javascript and the DOM... using document.createElement()

for an example, search for createElement with an author of my username: feyd
seahorse123
Forum Newbie
Posts: 8
Joined: Tue Sep 20, 2005 9:06 pm

Post by seahorse123 »

thanks, feyd.
However can I call php function in javascript? the php function generates form dropdown list. If I use "Create Element()" how to "copy" this dropdown menu box as new element?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you simply create a SELECT element and iterate the options array creating new copies of each as you go.. fairly simple..
seahorse123
Forum Newbie
Posts: 8
Joined: Tue Sep 20, 2005 9:06 pm

Post by seahorse123 »

Sorry I am still not quite understand, for example I have code:

Code: Select all

<?
$result1=mysql_query("select id,name from table");
print "<select name=abc>";
display();
print "</select>";
print "<input type=button value="add new dropdown" onclick="createnew()">";

function display() {
for ($i = 0; $i < @mysql_num_rows( $result1 ); ++$i)
{
$line = mysql_fetch_row($result1);
print "<option value=$line[0]>$line[1]</option>";
}	
}
?>
<script>
function createnew(){
    var new = document.createElement("<select  name=abc>")
?????
    document.body.insertBefore(new);
}
</script>
I can populate data into dropdown list, now if I create a new element through javascript, how to "copy" this dropdown menu when I click "add new dropdown" button? can you show me some sample code? thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basics..........

Code: Select all

var frm = document.forms['yourFormName'];
var opts = frm.elements['originalSelectName'].options;
var sel = document.createElement('SELECT');
for(var i = 0; i < opts.length; i++)
{
  sel.options[i] = new Option(opts[i].text, opts[i].value, false, opts[i].selected);
}
frm.appendChild(sel);
Post Reply