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

    How To Omit "undefined" Values

    My form keeps printing out the word "undefined" on submit, because of checkboxes that have been named and aren't checked. How do I stop the output page from printing out the value "undefined" on the webpage?

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When did you add this:
    I can't run the script because of the error it throws.

    requiredreturndoconhardcopy = unescape(params["requiredreturndoconhardcopy"]).replace(/\+/g, "");checkbox.checked;

    .....Willy

  • #3
    New Coder
    Join Date
    Feb 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Willy,

    That's not there anymore. A while ago, I had put that in and took it out, but forgot to upload the page again. Can you find the problem now?

  • #4
    New Coder
    Join Date
    Feb 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone who can be of assistance, please help. I still haven't gotten a response.

    Try filling out my form at melaniestype.com/serviceagreement.html and see what happens so you'll know what I'm talking about. How can I fix this problem? Pay special attention to the output section that reads "Return Finished Materials..."

    I only want the value for the checkbox that was checked when the form is submitted.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Unchecked checkboxes are not submitted (same as radio buttons and selects). You will notice that the value of the unchecked checkboxes are not present in the URL.
    So accessing it like params["requiredreturndocelectronically"] will return undefined. To avoid that, you must check if they are undefined.

    if (typeof requiredreturndoconhardcopy == "undefined") requiredreturndoconhardcopy = ""; //or set it to something else
    ...
    Glenn
    ____________________________________

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

  • #6
    New Coder
    Join Date
    Feb 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for replying, Glenn. But I had tried the code you said before now and it didn't work unless I'm putting it in the wrong place. I'm just testing the code right now, so I haven't put in the code for each checkbox that I want to affect. Here's what I have:

    <!-- Begin
    function getParams() {
    var idx = document.URL.indexOf('?');
    var params = new Array();
    if (typeof requiredreturndoconhardcopy == "undefined") {requiredreturndoconhardcopy = ""; //or set it to something else
    }
    if (idx != -1) {
    var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
    for (var i=0; i<pairs.length; i++) {
    nameVal = pairs[i].split('=');
    params[nameVal[0]] = nameVal[1];
    }
    }
    return params;
    }
    params = getParams();
    // End -->
    </script>

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You obviously put it at the wrong place
    You should put it somewhere in between where you set the variable and before you display its value.


    requiredreturndoconhardcopy = unescape(params["requiredreturndoconhardcopy"]).replace(/\+/g, " ");
    ...

    if (typeof requiredreturndoconhardcopy == "undefined")requiredreturndoconhardcopy = ""; //or set it to something else

    ...
    document.write("<tr><td width=325><font size=2 face=garamond>Return finished materials: " + requiredreturndoconhardcopy + " " + requiredreturndocondisk + " " + requiredreturndocelectronically + "</font></td><td width=325><font size=2 face=garamond>Return finished materials via: " + requiredreturndocviapostalmail + " " + requiredreturndocviainternet + " " + requiredreturndocviaFedEx + " " + requiredreturndocviacourier + "</font></td></tr></table>");
    Glenn
    ____________________________________

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

  • #8
    New Coder
    Join Date
    Feb 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey Glenn,

    For some reason, my browser (or whatever) wasn't reading any non-entered values as "undefined," so every time I tried to put in the code like you told me and where you told me it wouldn't work. So I opted to try this:


    requiredreturndoconhardcopy = unescape(params["requiredreturndoconhardcopy"]).replace(/\+/g, " ");
    ...

    if (typeof requiredreturndoconhardcopy != "no hard copy")requiredreturndoconhardcopy = ""; //or set it to something else

    ...
    document.write("<tr><td width=325><font size=2 face=garamond>Return finished materials: " + requiredreturndoconhardcopy

    ...


    Which is pretty much a reverse of the way you said to do it. And it worked! So I'm going to do it this way. If I just say NOT EQUAL TO [the preset element's value], then I shouldn't have any problems. Thanks for your help.

  • #9
    New Coder
    Join Date
    Feb 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I said earlier:

    For some reason, my browser (or whatever) wasn't reading any non-entered values as "undefined," so every time I tried to put in the code like you told me and where you told me it wouldn't work.


    -------------------

    Sorry, Glenn, I meant I put the code where you told me it would work. So you'll know that I did what you told me.

    Thanks for helping me earlier too.


  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Sorry, it was my mistake. The code should have been:

    if (requiredreturndoconhardcopy == "undefined")requiredreturndoconhardcopy = "";
    Glenn
    ____________________________________

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

  • #11
    New Coder
    Join Date
    Feb 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I see. That works too. I tried both ways. Thanks.

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    if you're going to use the first code, the typeof should be dropped since doing so will return "string", not the value of the string.

    if (requiredreturndoconhardcopy != "on hard copy") requiredreturndoconhardcopy = "";
    Glenn
    ____________________________________

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

  • #13
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    function getParams() {
    var idx = document.URL.indexOf('?');
    var params = new Array();
    if (typeof requiredreturndoconhardcopy == "undefined") {requiredreturndoconhardcopy = ""; //or set it to something else
    }
    if (idx != -1) {
    var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
    for (var i=0; i<pairs.length; i++) {
    nameVal = pairs[i].split('=');
    params[nameVal[0]] = nameVal[1];
    }
    }
    return params;
    }
    This will incorrectly parse certain query strings. An alterative parser can be found at http://www.webdevfaqs.com/javascript.php#querystring
    Last edited by Jeff Mott; 02-17-2004 at 05:16 AM.

  • #14
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Actually, I have already linked your thread on query string parser script to egquen2000 in this thread. I don't know why he keeps on using the old code.
    Glenn
    ____________________________________

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


  •  

    Posting Permissions

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