Log in

View Full Version : Dynamic form variables



junestag
10-03-2007, 05:34 PM
I've created dynamic fields using DHTML in my form page. But now I want to process those dynamic fields in my PHP form processing page and I'm unsure how to go about it.

I'm assuming some kind of for loop will allow me to go through my $_POST variables...?

Here's how I created the variables in my form page:



<script type="text/javascript">

x=0;

function insertRow(){

eLabel=document.createElement("label");
eLabel.setAttribute("id","label");
eLabel.setAttribute("for","DEPT"+x);

eDept=document.createElement("select");
eDept.setAttribute("id","dept"+x);
eDept.setAttribute("class","select");

eBR=document.createElement("br");
eBR2=document.createElement("br");

for (i=0; i < document.theForm.DEPT.length; i++) {
Option[i] = document.theForm.DEPT[i];
objOption = new Option(Option[i].text,Option[i].value);
eDept.options[eDept.length] = objOption;
}

document.getElementById("mContainer").appendChild(eLabel);
document.getElementById("mContainer").appendChild(eDept);
document.getElementById("mContainer").appendChild(eBR);
document.getElementById("mContainer").appendChild(eBR2);

x++;
}

</script>


So you see, the variables are named, in this example, 'dept' + the value of x in that iteration.

I guess what I'm asking is, how do you retrieve values from a form when you don't know how many values there will be ahead of time?

boogyman
10-03-2007, 05:48 PM
eLabel=document.createElement("label");
eLabel.setAttribute("id","label");
eLabel.setAttribute("for","DEPT"+x);

eDept=document.createElement("select");
eDept.setAttribute("id","dept"+x);
eDept.setAttribute("class","select");

dont forget the "name" properties of the form. that will become the key for processing later.




foreach($_POST as $k => $v)
{
// Process
}

djr33
10-03-2007, 06:13 PM
foreach will give every variable sent, including the send button, standard fields, etc.
The best idea for more control is to name the fields in a logical way so that you can find them.

Sequentially name them, for example, "dynamic1", "dynamic2", etc.

Then you can use a for loop:

for ($i=1;isset($_POST['dynamic'.$i];$i++) {
//do stuff
}

I believe there is also a way to send data as an array to PHP, using something like name="dynamic[1]", but I can't remember the details.
Then, $_POST['dynamic'] would be an array holding each of those.
I'd suggest researching this. If I come across it, I'll post again.

junestag
10-03-2007, 11:06 PM
Great, thanks!

tech_support
10-05-2007, 09:07 AM
foreach will give every variable sent, including the send button, standard fields, etc.
The best idea for more control is to name the fields in a logical way so that you can find them.

Sequentially name them, for example, "dynamic1", "dynamic2", etc.

Then you can use a for loop:

for ($i=1;isset($_POST['dynamic'.$i];$i++) {
//do stuff
}

I believe there is also a way to send data as an array to PHP, using something like name="dynamic[1]", but I can't remember the details.
Then, $_POST['dynamic'] would be an array holding each of those.
I'd suggest researching this. If I come across it, I'll post again.
You forgot to close the isset() :p

djr33
10-05-2007, 09:15 AM
Oops.
for ($i=1;isset($_POST['dynamic'.$i]);$i++) {