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 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checkbox values to textbox help

    Hi, I am trying to pass information to a text field before I submit and having trouble with the checkbox values - code is as follows;

    function choose_itin()
    {

    dest_field = document.forms["params"].elements["DEST"];
    dest_index = document.forms["params"].elements["DEST"].selectedIndex;
    dest_info = dest_field[dest_index].value;

    leaving_info = document.forms["params"].elements["LEAVING"].value;

    dep_info = document.forms["params"].elements["DEP"].value;


    // these next 3 seem to be the problem - works fine without them

    if (document.forms["params"].elements["TRF"].checked == true) {
    trf_info = 'Y';
    }
    else {
    trf_info = 'N';
    }

    if (document.forms["params"].elements["CAR"].value == true) {
    car_info = 'Y';
    }
    else {
    car_info = 'N';
    }

    if (document.forms["params"].elements["SIGHT"].value == true) {
    sight_info = 'Y';
    }
    else {
    sight_info = 'N';
    }

    document.forms["params"].elements["auto"].value = "DEST=" + dest_info + "#LEAVING=" + leaving_info + "#DEP=" + dep_info + "#TRF=" + trf_info + "#CAR=" + car_info + "#SIGHT=" + sight_info;

    }


    Any help with this is appreciated.
    BVT.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Can you describe the problem in more the detail? Showing the related HTML will also help.

    In any case, you can simplify your code by storing the form reference to a variable instead of accessing it everytime and leaving out unnecessary variables.
    Code:
    function choose_itin()
    {
    	var frm = document.forms["params"]
    	var dest_field = frm.elements["DEST"];
    	var dest_info = dest_field[dest_field.selectedIndex].value;
    
    	var leaving_info = frm.elements["LEAVING"].value;
    
    	var dep_info = frm.elements["DEP"].value;
    
    	...
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Jun 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK - sorry about that...

    The objective is to take the values from various select / text boxes and submit it via the 'auto'text field giving a result as follows;

    http://blahblah?auto=DEST=123#LEAVIN...e=&log_prefix=

    In the html DEST is a select box, LEAVING & DEP are text boxes and TRF, CAR & SIGHTS are checkboxes.

    The script works for the Select & text boxes - but something is wrong with the following 3 bits (all doing the same thing) 'auto' fails to have any values if they are included;

    if (document.forms["params"].elements["TRF"].checked == true) {
    trf_info = 'Y';
    }
    else {
    trf_info = 'N';
    }

    if (document.forms["params"].elements["CAR"].value == true) {
    car_info = 'Y';
    }
    else {
    car_info = 'N';
    }

    if (document.forms["params"].elements["SIGHT"].value == true) {
    sight_info = 'Y';
    }
    else {
    sight_info = 'N';
    }


    Have attached a simple version of the form - hope this helps...

    Cheers and thanks for the hint re from reference.
    Attached Files Attached Files
    BVT.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Why do you want to do it manually when the form GET method already does that?

    When you submit the page, the form data is automatically sent to the URL like this:

    http://blahblah?DEST=1&LEAVING=blah&DEP=blahblah&...and so on

    Take note that uncheck checkboxes are not sent to the URL and if you don't define a value for it, the default value is 'ON'.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    Jun 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah - that would be nice...

    But I need to have it passed through the 'auto' hidden field value giving -

    auto=DEST=1#DEP=blah#LEAVING=blah etc.

    Also re checkbox - though that the code was checking if the checkbox was checked and if it was assigning tfr_info with 'Y' or if not assigning 'N' (using the boolean value true / false for the check). Not sure of the syntax for this - is what I have correct (can't be or it would work...).

    Thanks for your help so far - appreciated!
    BVT.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    What you're asking is easy but I want to know first why do you need the 'auto' field. Is there any special reason you want to do it that way? As I said, you can let the browser do it automatically without scripting it.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    New Coder
    Join Date
    Jun 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The info is passed to another program which is looking for it in this format (ie. via the auto field) - some responses can go as per normal and the others have to go via the auto field.

    Thanks.
    BVT.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    function choose_itin(f)
    {
        var dest_field = f.elements["DEST"];
        var dest_info = dest_field[dest_field.selectedIndex].value;    
        var leaving_info = f.elements["LEAVING"].value;    
        var dep_info = f.elements["DEP"].value;        
        var trf_info = (f.elements["TRF"].checked) ? 'Y':'N';
        var car_info = (f.elements["CAR"].checked) ? 'Y':'N';
        var sight_info = (f.elements["SIGHT"].checked) ? 'Y':'N';
            
        f.elements["auto"].value = "DEST=" + dest_info + "#LEAVING=" + leaving_info + "#DEP=" + dep_info + "#TRF=" + trf_info + "#CAR=" + car_info + "#SIGHT=" + sight_info; 
        
    }
    ...
    <form ... onsubmit="choose_itin(this)">
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    New Coder
    Join Date
    Jun 2004
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fantastic - really appreciate the help!

    Cheers
    BVT.


  •  

    Posting Permissions

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