how ti limit the additions field?

Discussion in 'HTML & Website Design' started by s.jns, Oct 30, 2010.

  1. #1
    Hi,
    If someone here is willing to help me I would really appreciate

    how to limit the addition of columns in the following html code, let say only up to 5 only?
          <form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="friends"
              <table width="100%" cellpadding="10" cellspacing="0" id="my_friends">
                <tr>
                  <th colspan="2" style="text-align: center;"><?php echo $text_enter_friend; ?></th>
                </tr>
                <tr>
                  <td class="left" width="30%"><span class="required">*</span> <?php echo $entry_friend; ?></td>
                  <td class="left"><input type="text" name="friend" value="<?php echo $friend; ?>" size="30" maxlength="45" />
                    <?php if ($error_friend) { ?>
                    <span class="error"><?php echo $error_friend; ?></span>
                    <?php } ?></td>
                </tr>
                <?php if ($friends) { ?>
                <?php foreach ($friends as $result) { ?>
                <tr>
                  <td style="vertical-align: top;"><?php echo $entry_friend; ?></td>
                  <td style="vertical-align: top;"><input type="text" name="friends[]" value="<?php echo $result; ?>" size="30" maxlength="45" /></td>
                </tr>
                <?php } ?>
                <?php } ?>
              </table>
              <table width="100%" cellpadding="10" cellspacing="0">
                <tr>
                  <td></td>
                  <td class="right"><a onclick="addFriend();" class="button"><span><?php echo $button_add_friend; ?></span></a><a onclick="removeFriend();" class="button"><span><?php echo $button_remove; ?></span></a></td>
                </tr>
                <tr>
                  <td colspan="2" class="center">
                    <p><?php echo $text_message; ?></p>
                    <p><a onclick="$('#friends').submit();" class="button"><span><?php echo $button_submit; ?></span></a></p>
                    <p><?php echo $text_addresses; ?></p>
                  </td>
                </tr>
              </table>
            </div>
          </form>
    
    Code (markup):
    
    <script type="text/javascript"><!--
    function addFriend() {
    	var tbl = document.getElementById('my_friends');
    	var iteration = tbl.tBodies[0].rows.length;
    	newRow = tbl.tBodies[0].insertRow(-1);
    	var newCell = newRow.insertCell(0);
    	newCell.innerHTML = '<?php echo $entry_friend; ?>';
    	var newCell1 = newRow.insertCell(1);
    	var el = document.createElement('input');
    	el.type = 'text';
    	el.name = 'friends[]';
    	el.size = 30;
    	el.maxlength = 45;
    	newCell1.appendChild(el);
    //	if (newCell > 2) tbl.addCell(newCell + 1);
    }
    
    function removeFriend() {
    	var tbl = document.getElementById('my_friends');
    	var lastRow = tbl.rows.length;
    	if (lastRow > 2) tbl.deleteRow(lastRow - 1);
    }
    //--></script>
    
    Code (markup):

    Thanks in advance
     
    Last edited: Oct 30, 2010
    s.jns, Oct 30, 2010 IP
  2. S1M

    S1M Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If additional cells can only be added in a single session, increment a counter to set your limit. Otherwise, inspect the dom to see how many cells are in the row and exit the function if it's >5.
     
    S1M, Oct 30, 2010 IP
  3. s.jns

    s.jns Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Thanks S1M
     
    s.jns, Oct 30, 2010 IP