Copyme - multiple variables

Discussion in 'JavaScript' started by Weirfire, Jun 30, 2006.

  1. #1
    I would say I had average Javascript skills so I'm hoping there's someone out there that can help me with this problem.


    I basically have an admin page which has a form at the top for adding new entries and a list of previous entries below it. I've made a copy function which when you click the button beside the previous entries it fills the top form with the previous data..... only it just gives me the values of the first form.

    The copyme function goes something like

    	function copyme() {
    		var a = document.getElementById('bar').value
    		document.getElementById('foo').value = a;
                 }
    Code (markup):
    The forms page goes something like

    print "<form action='$PHP_SELF' method='POST'>
    <tr>
      <td>foo</td>
      <td><input id='foo' type='text' name='foo' size='30' /></td>
    </tr>
    </form>";
    
    print "<form action='$PHP_SELF' method='POST'>
      <tr>
        <td><input type='text' name='bar' id='bar' value='1' /></td>
        <td><input type='button' onclick='copyme()' value='copy' /></td>
      </tr>
      <tr>
        <td><input type='text' name='bar' id='bar' value='2' /></td>
        <td><input type='button' onclick='copyme()' value='copy' /></td>
      </tr></form>";
    PHP:
    How do I get 2 to appear on the above form if I click copy?
     
    Weirfire, Jun 30, 2006 IP
  2. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #2
    You need a container and then add your form elements to the container. I too am not a javascript expert, but did something similar this week. I used this to create a working dynamic vBPicGallery edit page. The members can upload photos, delete photos, and change photo descriptions dynamically right on the page adding and removing photo elements as necessary. No page reloads.

    http://www.dustindiaz.com/add-and-remove-html-elements-dynamically-with-javascript/
     
    noppid, Jun 30, 2006 IP
    Weirfire likes this.
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Thats a pretty sweet script you built. Can I see it? :)

    Thanks for the tips nop. I'll see if I can get this working. I dont actually know any professional javascript programmers. Its one of those things thats handy for some stuff but you wouldn't really want to specialise in it. lol

    The guy that wrote the tutorial is pretty funny :)

    And now its just confusing me! :confused: lol
     
    Weirfire, Jun 30, 2006 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    okay I didn't use the complicated example to work out it out but I hashed through it myself and stumbled across the answer.

    Basically I used the id of the rows being printed to create some hidden fields with the id as the name. The problem was that we had several forms using the same id reference. I then passed the id of the row through the copy me function and added the ids to the field names which were referenced by the hidden fields.

    You could probably do it by setting the form name to a specific id but I'm too inexperienced to work it out. If someone knows how to do it by form id then I'd like to hear how as it would save 10 new hidden fields being created.
     
    Weirfire, Jun 30, 2006 IP