Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help with dynamic form

    hi

    i create a form using jquery. i actually found jquery code from other website and modified it according to my needs. i need help in how can i get data in side all these fields to php file. on php side i am just echoing this data.

    html form
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
     
        <script type="text/javascript">
            $(document).ready(function() {
    
                $('#btnAdd').click(function() {
       
                    var num     = $('.clonedInput').length;
                
                    var newNum  = new Number(num + 1);
           
                    var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
            
                    newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
                    $('#input' + num).after(newElem);
                });
             
            });
        </script>
        <style>
    #myForm div {
       font-size:14px;
       margin-bottom: 10px;
       clear: left;
    }
    #myForm label {
       width: 125px;
       display: block;
       font-size:14px;
       font-weight: bold;
       color: #999;
       float: left;
    }
    #btnAdd {
       margin-left:130px;
    }
    </style>
    
    </head>
     
    <body>
     
    <form id="myForm" method="post" action="process.php">
       <div>
        <label>Firstname: </label><input type="text" name="fname" id="fname"><br>
        </div>
        <div>
        <label>lastname: </label><input type="text" name="lname" id="lname"><br>
        </div>
        <div id="input1" class="clonedInput">
          <label>IM Screen Names:</label>
            <input type="text" name="name1" id="name1" />
            <select name="screenname1" id="screenname1">
              <option value="AIM" selected="selected">AIM</option>
              <option value="gtalk">Google Talk</option>
              <option value="skype">Skype</option>
              <option value="windows live">Windows Live</option>
              <option value="yahoo">Yahoo</option>
            </select><br />
        </div>
     
        <div>
           <a href="#" id="btnAdd">Add another</a>
        </div>
        <input type="submit" name="submit" value="Submit">
    </form>
     
    </body>
    </html>
    process.php
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $name1 = $_POST['name1'];
    $screenname1 = $_POST['screenname1'];
    
    echo $fname."<br>";
    echo $lname."<br>";
    echo $name1."<br>";
    echo $screenname1."<br>";
    
    ?>
    </body>
    </html>

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Are you asking how to send the form data to the PHP script via Ajax?

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bdl View Post
    Are you asking how to send the form data to the PHP script via Ajax?
    i am not using ajax. just normal post method

  • #4
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Looks to me like the field will be numbered sequentially so you would need to check

    PHP Code:
    $name2 $_POST['name2'];
    $screenname2 $_POST['screenname2'];
    $name3 $_POST['name3'];
    $screenname3 $_POST['screenname3']; 
    or more realistically as there could be unlimited presumably load them into an array in a loop, something like:

    PHP Code:
    $intField 1;
    $arrNames = array();
    $arrScreenNames = array();
    while(isset(
    $_POST['name'.$intField])) {
      
    $arrNames[] = $_POST['name'.$intField];
      
    $arrScreenNames[] = $_POST['screenname'.$intField];
      
    $intField++;

    HTH,

    Dai


  •  

    Posting Permissions

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