www.webdeveloper.com
Results 1 to 13 of 13

Thread: How do I populate a form field from another window?

  1. #1
    Join Date
    Apr 2005
    Posts
    8

    How do I populate a form field from another window?

    Here's my scenario:

    Main Window:
    I have a field called Zipcode
    There is a link that opens a new window which enables me to search for zip codes.

    ZipCode Window:
    I can enter the name of a city and a list of zip codes show up.


    Here's what I want:
    I want to be able to click on the zip code which was found in the ZipCode Window and have that number be automatically populated in the Zipcode field which is in the Main Window

    Any help greatly appreciated!

  2. #2
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    let's say you have
    Code:
    <form name="zipform" action="nextpage.php" method="post">
    <input name="zipcode" id="zipcode" type="text">
    ...
    </form>
    in your main window.

    In the popup window, you would have links like
    Code:
    <a href="#" onclick="window.opener.document.getElementById('zipcode').value='12345';window.close();">12345</a>
    The forum has put some spaces in that shouldn't be there in v alue
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  3. #3
    Join Date
    Apr 2005
    Posts
    8
    Hey great thanks! This is working good There's only one snag...I failed to state that there will be 3 zip code fields in the main window. I need to be able to fill them in independantly using this feature.

    I'm using PHP

    What do you reccomend?

    Thanks!
    Last edited by jaytee; 04-15-2005 at 01:25 AM.

  4. #4
    Join Date
    Apr 2005
    Posts
    8
    Wow, this forum moves fast! Anyways, just gonna ask another time before this post gets lost in ether

    So how would I go about populating 3 different zip code fields independantly?

    Thanks alot...any help greatly appreciated!

  5. #5
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Sorry I'm not at your beck and call. I had to go to bed last night and then I had to go to work this morning. Now it's lunch time.

    You would give each form field a unique id (zipcode1, zipcode2, zipcode3) and then (I'm assuming you open the window for one and make the selection, then open it for the next one, etc) you could have the id be a querystring variable passed to the popup window and use it to determine which form field gets the value

    Main window, revised
    Code:
    <form name="zipform" action="nextpage.php" method="post">
    <p><input name="zipcode1" id="zipcode1" type="text"> <a href="popup.php?fieldId=zipcode1" target="_blank">Choose from a list</a></p>
    <p><input name="zipcode2" id="zipcode2" type="text"> <a href="popup.php?fieldId=zipcode2" target="_blank">Choose from a list</a></p>
    <p><input name="zipcode3" id="zipcode3" type="text"> <a href="popup.php?fieldId=zipcode3" target="_blank">Choose from a list</a></p>
    ...
    </form>
    Popup window, revised
    Code:
    <a href="#" onclick="window.opener.document.getElementById('<?php echo $_GET["fieldId"]; ?>').value='12345';window.close();">12345</a>
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  6. #6
    Join Date
    Apr 2005
    Posts
    8

    Thumbs up

    Hey, I hope I didn't come off as an ass I'm just new to this forum and didn't realize how quickly things move around here and was affraid it was going to get lost. I'm sorry for the misunderstanding

    Anyways, thank you very very very much for your help! You are a code wizard

    Sincerely,
    jaytee

  7. #7
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    Don't worry, I was only kidding
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  8. #8
    Join Date
    Apr 2005
    Posts
    8
    Oh and HaganeNoKokoro, you thought it was over? No!! Haha. Anyways, was wondering howi can get the zipcode finder window to open a new window at a fixed size and positioned in the center of the screen. I tried using DreamWeavers Open Window behavior, but it screws up the code and doesnt work.

    Anyways, any help greatly appreciated (again)
    Thanks

  9. #9
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    for that, you will need a function like
    Code:
    <script type="text/javascript">
    function popup(url, x, y, w, h) {
      var params="x="+x+", y="+y+", width="+width+", height="+height;
      window.open(url, "_blank", params);
    }
    </script>
    And then the links that open the windows will be like
    Code:
    <a href="#" onclick="popup('popup.php?fieldId=zipcode1', 100, 100, 200, 400);return false;">Choose from a list</a>
    Of course you will have to change the x, y, w, and h to suit where you want the window and how big. I don't know if there's a way to make it appear in the exact center of the screen, although you could use screen.width and screen.height to calculate it.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  10. #10
    Join Date
    Apr 2005
    Posts
    8
    Wow thanks man! This is great but..............

    You're really going to hate me now!!

    I found the ultimate way that I would like for this to work. Instead of clicking "Find This Zip" for all 3 fields, you can just click these checkboxes and do all 3 at once.

    Please check out this perfect example here:
    hxxp://agentmachine.com/realestate/buyhome.asp

    That's EXACTLY what I want. But I don't think I can just lift the code from that site. Do you know of any similar ways of working it? Any idears?

    Thanks!!!!!!!!!!!!!!!!!!!

  11. #11
    Join Date
    Jul 2004
    Location
    Canada, eh
    Posts
    784
    For that you will need something like this
    Code:
    <script type="text/javascript">
    function submitForm(frm) {
      var cur=1;
      for(var i=0; i<frm.elements.length; i++) {
        if(frm.elements[i].type=="checkbox") {
          if(frm.elements[i].checked) {
            window.opener.document.getElementById("zipcode"+(cur++)).value=frm.elements[i].value;
          }
          if(cur>4) break; //put the number of fields you want fileld here
        }
      }
      window.close();
    }
    </script>
    in the popup window. The form in the popup will also need a button
    Code:
    <input type="button" onclick="submitForm(this.form)">
    for the user to click when they're done.

    If you also want the "You can only select 4 zipcodes" message to pop up, youo will need to add an onclick handler to the checkboxes. put this function
    Code:
    function validate(frm) {
      var cnt=0;
      for(var i=0; i<frm.elements.length; i++) {
        if(frm.elements[i].type=="checkbox"&&frm.elements[i].checked) cnt++;
        if(cnt>4) {
          alert("You can only select 4 zipcodes");
          frm.elements[i].checked=false;
          break;
        }
      }
    }
    with the other one, and have checkboxes like this
    Code:
    <input type="checkbox" name="somezip" value="12345" onclick="validate(this.form)">
    I haven't tested out any of this so there will probably be problems.
    Last edited by HaganeNoKokoro; 04-21-2005 at 11:14 PM.
    Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.

  12. #12
    Join Date
    Apr 2005
    Posts
    8
    Holy crap! You are amazing! I'm gonna have to give this a try tomorrow. Thanks a billion! I'll let you know if I got it to work.
    Thanks again!

  13. #13
    Join Date
    May 2003
    Posts
    5
    nvm
    Last edited by shawjames; 08-10-2012 at 11:41 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles