![]() |
| HTML | PHP Scripts | Webmaster Tools | Webmaster Forums | Web Hosting | Domain Names | Webmaster Books |
|
|
#1 |
|
Junior Member
Join Date: Dec 2008
Location: Sydney, Australia
Webmaster Discussions: 27
Rep Power: 11 ![]() |
I'm in need of some help. currently working on a major php project for uni and am slightly stuck on a form feature that is required.
i've got a drop down menu and an input box next to it. once the user clicks the add button, I need a new drop down and input box to be generated under it. this new row of fields will occur each time they click on the add. |
|
|
|
|
|
#2 |
|
bald headed old fart
Join Date: Aug 2003
Location: chertsey, a small town 25 miles south west of london, england.
Webmaster Discussions: 580
Rep Power: 67 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: Help required urgently
Hi there Bigmous,
here is an example that I have just knocked up... Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title></title>
<style type="text/css">
#theForm div {
clear:both;
width:320px;
height:20px;
padding:10px;
border:1px solid #333;
margin:4px auto;
}
#theForm input,#theForm select {
float:left;
margin:0 5px 10px;
}
.blue {
background-color:#ccf;
}
.red {
background-color:#fcc;
}
</style>
<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',init,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',init);
}
}
function init(){
c=0;
d=2
optLength=3;
opt=[];
df=document.forms[0];
df[2].onclick=function(){
addElements();
}
}
function addElements() {
c++;
dv=document.createElement('div');
dv.setAttribute('id','container'+c);
if(c%2==0) {
dv.className='blue';
}
else {
dv.className='red';
}
sel=document.createElement('select');
sel.setAttribute('name','select'+c);
for(k=0;k<optLength;k++){
d++;
opt[k]=document.createElement('option');
opt[k].setAttribute('value',d);
if(k==0){
opt[k].appendChild(document.createTextNode('---options---'));
}
else {
opt[k].appendChild(document.createTextNode('option '+d));
}
sel.appendChild(opt[k]);
}
dv.appendChild(sel);
inp=document.createElement('input');
inp.setAttribute('name','input'+c);
inp.setAttribute('type','text');
dv.appendChild(inp);
document.forms[0].appendChild(dv);
}
</script>
</head>
<body>
<form id="theForm" action="#">
<div id="container0" class="blue">
<select name="select0">
<option value="0">---options---</option>
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
<input name="input0" type="text">
<input type="button" value="add">
</div>
</form>
</body>
</html>
__________________
|
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Dec 2008
Location: Sydney, Australia
Webmaster Discussions: 27
Rep Power: 11 ![]() |
Re: Help required urgently
Thanks heaps mate, the only problem i'm trying to figure out now is how i can keep the add button next to or under the last 1
Cheers, Mous |
|
|
|
|
|
#4 |
|
bald headed old fart
Join Date: Aug 2003
Location: chertsey, a small town 25 miles south west of london, england.
Webmaster Discussions: 580
Rep Power: 67 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: Help required urgently
Hi there Mous,
this can be easily done with a slight modification. ![]() Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title></title>
<style type="text/css">
#theForm {
margin:0;
}
#theForm div {
clear:both;
width:264px;
height:20px;
padding:10px;
border:1px solid #333;
margin:4px auto;
}
#theForm input,#theForm select {
float:left;
margin:0 5px 10px;
}
#button {
width:284px;
margin:auto;
}
#add {
float:right;
}
.blue {
background-color:#ccf;
}
.red {
background-color:#fcc;
}
</style>
<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',init,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',init);
}
}
function init(){
c=0;
d=2
optLength=3;
opt=[];
df=document.forms[0];
document.getElementById('add').onclick=function(){
addElements();
}
}
function addElements() {
c++;
dv=document.createElement('div');
dv.setAttribute('id','container'+c);
if(c%2==0) {
dv.className='blue';
}
else {
dv.className='red';
}
sel=document.createElement('select');
sel.setAttribute('name','select'+c);
for(k=0;k<optLength;k++){
d++;
opt[k]=document.createElement('option');
opt[k].setAttribute('value',d);
if(k==0){
opt[k].appendChild(document.createTextNode('---options---'));
}
else {
opt[k].appendChild(document.createTextNode('option '+d));
}
sel.appendChild(opt[k]);
}
dv.appendChild(sel);
inp=document.createElement('input');
inp.setAttribute('name','input'+c);
inp.setAttribute('type','text');
dv.appendChild(inp);
document.forms[0].appendChild(dv);
}
</script>
</head>
<body>
<form id="theForm" action="#" >
<div id="container0" class="blue">
<select name="select0">
<option value="0">---options---</option>
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
<input name="input0" type="text">
</div>
</form>
<div id="button">
<input id="add" type="button" value="add">
</div>
</body>
</html>
__________________
|
|
|
|
|
|
#5 |
|
Junior Member
Join Date: Dec 2008
Location: Sydney, Australia
Webmaster Discussions: 27
Rep Power: 11 ![]() |
omg ofcourse, so very simple!!! lol thanks heaps, i think lack of sleep and all my c++, java and php projects are finally getting to me. haha how could i of missed that.
thanks again
|
|
|
|
|
|
#6 |
|
bald headed old fart
Join Date: Aug 2003
Location: chertsey, a small town 25 miles south west of london, england.
Webmaster Discussions: 580
Rep Power: 67 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: Help required urgently
No problem, you're very welcome, Mous.
__________________
|
|
|
|
![]() |
| Bookmarks |
| Tags |
| required, urgently |
| Thread Tools | |
| Rate This Thread | |
|
|
Similar Webmaster Discussions
|
||||
| Thread | Webmaster Discussion Starter | Forum | Replies | Last Post |
| Help required about media | itmodo | Archive | 0 | 06-26-2009 07:21 AM |
| Freelancers wanted urgently that specialize in the following IT fields... | ServiceNinja | Internet Services | 0 | 07-12-2006 09:52 PM |
| Wanted Urgently : Website | tuankeedin | Websites For Sale | 2 | 02-20-2006 03:39 AM |