How to handling dinamic add field in this form

Discussion in 'PHP' started by junandya, Sep 2, 2009.

  1. #1
    Hello,

    I need help how to handling & insert the each values from this form below to mysql. I really have no idea.

    
    <html>
    <head>
    <title></title>
    <script language="javascript">
    fields = 0;
    function addInput() {
    if (fields != 10) {
    document.getElementById('text').innerHTML += "<input type='text' value='' /><br />";
    fields += 1;
    } else {
    document.getElementById('text').innerHTML += "<br />Only 10 upload fields allowed.";
    document.form.add.disabled=true;
    }
    }
    </script>
    </head>
    <body>
    <form name="form" method="post" action="engine/proc_add.php">
    <input type="button" onclick="addInput()" name="add" value="Add input field" />
    </form>
    <div id="text">
    
    </div>
    </body>
    </html>
    
    HTML:
    Thank you for any help
     
    junandya, Sep 2, 2009 IP
  2. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #2
    You have closed the <form> tag before the start of your <DIV > tag that adds the text fields dynamically.. .Please try pasting the
     <div id="text">
    
    </div>
    PHP:
    before the </form> tag and try again. :)
     
    killerj, Sep 2, 2009 IP
  3. zeenatnaheed

    zeenatnaheed Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Change your code
    <html>
    <head>
    <title></title>
    <script language="javascript">
    fields = 0;
    function addInput()
    {
    if (fields != 10)
    {
    document.getElementById('text').innerHTML += "<input type='text' name='text"+fields+"' value='' /><br />";
    fields += 1;
    }
    else
    {
    document.getElementById('text').innerHTML += "<br />Only 10 upload fields allowed.";
    document.form.add.disabled=true;
    }
    }
    </script>
    </head>
    <body>
    <form name="form" method="post" action="engine/proc_add.php">
    <input type="button" onclick="addInput()" name="add" value="Add input field" />
    <div id="text"></div>
    </form>
    </body></html>

    In the file proc_add.php write

    <?php
    //open connection with mysql and select the database
    for($i=0$i<10;$i++)
    {
    $text=$_POST['text'.$i];
    if($text!="")
    {
    //use the value in $text to add value to the fields.
    }
    }
    ?>
    I hope this sorts out your problem
     
    zeenatnaheed, Sep 2, 2009 IP