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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Using Javascript to kludge together a nested form

    I have a maintenance page which loops through records and displays each field as an editable text box - there's one Submit button at the end which allows changes to all or any of the data on the page:

    Code:
    // Pseudocode for simplicity
    <form>
    while ($d = mysql_fetch_assoc($sql)) {
    <input type="text" name="date" value="$d['date']" />
    <input type="text" name="1" value="$d['val1']" />
    <input type="text" name="2" value="$d['val2']" />
    [...]
    }
    <submit button>
    </form>
    I want to be able to add a "duplicate this record" option at the end of each line, which will allow you to select a new date and then copy all the rest of the data from that line into that date. Since it will already be inside a <form>, I can't put this in another <form>, so I'm trying Javascript:

    Code:
    // Pseudocode for simplicity
    <form>
    while ($d = mysql_fetch_assoc($sql)) {
    <input type="text" name="date" value="$d['date']" />
    <input type="text" name="1" value="$d['val1']" />
    <input type="text" name="2" value="$d['val2']" />
    
    <input type="text" value="" name="dupdate<?=$d['ID']?>" id="dupdate<?=$d['ID']?>" onchange="document.getElementById('butt').onClick = 'parent.location = \'dupDetail.php?id=<?=$d['ID']?>&dt=this.value\';' ">
          <input type="button" id="butt" value="Go" onClick = "parent.location = 'dupDetail.php?id=<?=$d['ID']?>' " />
    [...]
    }
    <submit button>
    </form>
    I think the sticking point is the changing "parent.location" bit. All I need is that when the text box is changed, the URL the button redirects to will change to include this new value. It doesn't give any error messages in the console (I'm using Firefox at the moment) so with my limited Javascript I'm a bit stuck!

  • #2
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FWIW - think I've solved it with a separate function and eval():

    Code:
    <input type="button" id="butt" value="Go" onClick="whiskaway('<?=$d['ID']?>');" />
    
    <script>
    function whiskaway(n) {
      var f = "dupDetail.php?id=<?=$d['ID']?>&dt=" + eval("document.getElementById('dupdate<?=$d['ID']?>').value");
      parent.location=f;
    }
    </script>

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    1. You cannot give a name or id to any element that begins with a number; it must begin with a-z or an underscore.
    2. eval() is evil, do not use it, it is JavaScripts executable environment and not only is it slow, but it opens your site to security issues.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".


  •  

    Tags for this Thread

    Posting Permissions

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