Results 1 to 6 of 6

Thread: Dynamic form variables

  1. #1
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Dynamic form variables

    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:

    Code:
    <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?

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    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.


    PHP Code:
    foreach($_POST as $k => $v)
    {
    // Process


  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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:
    PHP Code:
    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Oct 2007
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Great, thanks!

  5. #5
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Quote Originally Posted by djr33
    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:
    PHP Code:
    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()
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oops.
    for ($i=1;isset($_POST['dynamic'.$i]);$i++) {
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •