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

    Set Variable from Input

    Ok, I'm sure this is a really easy question, but I'm pretty new to the javascript/ajax world. What I have is a form that has 2 autocompleters. After the first input field is set, I want the 2nd autocompleter to pass the first input's data to the server script so I can run my SQL query based on their first input.

    <input name="title" type="text" id="title">
    <div id='current_titles' class='autocompleter'></div>
    <script ype="text/javascript">
    new Ajax.Autocompleter("title","current_titles","title_results.php");
    </script>

    <input name="who_said" type="text" id="who_said" />
    <div id='current_who' class='autocompleter'></div>
    <script type="text/javascript">
    new Ajax.Autocompleter("who_said","current_who","who_said.php");
    </script>

    I simply want to pass whatever they type into "title" as a $_GET variable attached to who_said.php. So..if they type "monkey" I want the 2nd autocompleter to pass "who_said.php?title=monkey"

    I've been trying to get the getElementById() function to do it, but I can't seem to get it to work, and don't know how to print things out with javascript so I can test it. document.write() doesn't seem to do it. I'm used to PHP, I just want a PRINT.

    Any help is greatly appreciated. Thanks!

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You need to call the code onchange of the form element and not with inline JavaScript.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, I was on to something, but now it's not working...I've tried this...

    var weburl;
    function setTitle(id){
    weburl = "who_said.php?title=";
    var current_title = document.getElementById(id).value;
    weburl += current_title;
    ##For Testing document.getElementById(id).value=weburl;
    }

    <input name="title" type="text" id="title" size="40" onchange="setTitle(this.id)"/>

    The first couple times I did it, this was printing the proper string into the input box. Now it does nothing. It doesn't make any sense? I undid all my changes, Ctrl + Z until it was back to normal and it still doesn't work.

    The autocompleter still wasn't working though, but I still have more ideas to try, but can't continue until I get this part working again.
    Last edited by andyrue; 07-22-2007 at 03:11 PM.

  • #4
    New to the CF scene
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I figured out why my code stopped working. Turns out when I would use text that came back from my autocompleter and then tab to the next field, onChange() doesn't get called. It only triggers if I press tab before the autocompleter responds. Is there anything I can do about this? Thanks

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Call the function manually from your autocomplete code.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

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