Page 1 of 1
Add new textfields
Posted: Wed Sep 21, 2005 11:33 am
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.
Posted: Wed Sep 21, 2005 11:47 am
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
Posted: Wed Sep 21, 2005 12:30 pm
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?
Posted: Wed Sep 21, 2005 12:36 pm
by feyd
you simply create a SELECT element and iterate the options array creating new copies of each as you go.. fairly simple..
Posted: Wed Sep 21, 2005 12:54 pm
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.
Posted: Wed Sep 21, 2005 1:12 pm
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);