...

View Full Version : How To Omit "undefined" Values



egquen2000
02-02-2004, 05:59 AM
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?

Willy Duitt
02-02-2004, 06:13 AM
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

egquen2000
02-02-2004, 06:22 AM
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?

egquen2000
02-02-2004, 08:20 AM
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.

glenngv
02-02-2004, 08:47 AM
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
...

egquen2000
02-02-2004, 08:58 AM
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>

glenngv
02-02-2004, 09:16 AM
You obviously put it at the wrong place :rolleyes:
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>");

egquen2000
02-02-2004, 09:56 AM
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.

egquen2000
02-02-2004, 10:00 AM
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.

:thumbsup:

glenngv
02-02-2004, 10:15 AM
Sorry, it was my mistake. The code should have been:

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

egquen2000
02-02-2004, 10:21 AM
OK, I see. That works too. I tried both ways. Thanks.

glenngv
02-02-2004, 10:33 AM
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 = "";

Jeff Mott
02-02-2004, 04:37 PM
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

glenngv
02-03-2004, 03:14 AM
Actually, I have already linked your thread on query string parser script to egquen2000 in this thread (http://www.codingforums.com/showthread.php?s=&threadid=32549). I don't know why he keeps on using the old code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum