Jump to content

help in dynamic form


doforumda

Recommended Posts

hi i got some code from another site. i modified it according to my needs. what is it doing it adds dynamic form fields by clicking "add another" link. the problem is when logged in user wants to modify his contact details he clicks on edit then gets to this modification form where all the data in his db is selected and displayed in that form fields. so the problem here is when he clicks on add another link it adds up that many fields sets that appeared currently. means if there appeared four sets of fields then clicking on add another link will add four more fields. what i want is to add only one set of fields which contains of text field and select menu.

 

my code is here

<!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>First Name: </label><input type="text" name="fname" id="fname"><br>
    </div>
    <div>
    <label>Last Name: </label><input type="text" name="lname" id="lname"><br>
    </div>
    <?php
   $connect = mysql_connect("localhost","user","pass");
   mysql_select_db("db");
   $get_im = mysql_query("SELECT * FROM contactim WHERE userid='$_SESSION[userid]'");
   $count = mysql_num_rows($get_im);
   while($get_contactim = mysql_fetch_assoc($get_im)) {
      $userim_db = $get_contactim['username'];
      $im_db = $get_contactim['im'];
      //echo $userim_db."<br>";
      
      for($i = 1; $i <= $count; $i++) {
   ?>
    <div id="input1" class="clonedInput">
      <label>IM Screen Names:</label>
        <input type="text" name="name[]" id="name1" value="<?php if($userim_db == "") { echo ""; } else { echo $userim_db; } ?>" />
        <select name="screenname[]" id="screenname1">
          <option value="AIM" <?php if($im_db == "AIM") { echo "SELECTED"; } ?>>AIM</option>
          <option value="gtalk" <?php if($im_db == "gtalk") { echo "SELECTED"; } ?>>Google Talk</option>
          <option value="skype" <?php if($im_db == "skype") { echo "SELECTED"; } ?>>Skype</option>
          <option value="windows" <?php if($im_db == "windows") { echo "SELECTED"; } ?>>Windows Live</option>
          <option value="yahoo" <?php if($im_db == "yahoo") { echo "SELECTED"; } ?>>Yahoo</option>
        </select><br />
        <?php
      }
   }
      ?>
    </div>

    <div>
       <a href="#" id="btnAdd">Add another</a>
    </div>
    <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.