Form value shows as Undefined

This is a discussion on "Form value shows as Undefined" within the JavaScript Forum section. This forum, and the thread "Form value shows as Undefined are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old May 23rd, 2006, 13:31
New Member
Join Date: May 2006
Location: Suffolk, United Kingdom
Age: 47
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Question Form value shows as Undefined

I have a form that is a basic user details address etc... There are 3 address fields on the form, 2 are required and one is optional. When the form is submitted, the following (confirmation) page shows "Undefined" where the empty 3rd address field is passed in. How can I use javascript in the confirmation page to ignore the address3 variable if it is empty or null?

All I can get it a blank line in between the address information like so:

Name
This House
This Street
undefined
This Town
This County
Postcode
Email

Where what I would like is:

Name
This House
This Street
This Town
This County
Postcode
Email

If there was a 3rd address line, this still needs to be shown in the confirmation page. This may be a really simple thing, but I'm a bit new to this - sorry!
Reply With Quote

  #2 (permalink)  
Old May 23rd, 2006, 22:04
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Form value shows as Undefined

It's probably not a question of using JavaScript. How are you processing the form information?
Reply With Quote
  #3 (permalink)  
Old May 24th, 2006, 07:43
New Member
Join Date: May 2006
Location: Suffolk, United Kingdom
Age: 47
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Re: Form value shows as Undefined

Here's the code in a simple page, the passed values would be for example: "Address1=The+Cottage&Address2=The+Street&Address3 =&Town=The+Village"

[CODE START]
<html>
<head>
<title>Test</title>
<script language="JavaScript">
function parseGetVars() {
var getVars = new Array();
var qString = top.location.search.substring(1);
var pairs = qString.split(/\&/);
for (var i in pairs) {
var nameVal = pairs[i].split(/\=/);
getVars[unescape(nameVal[0])] = unescape(nameVal[1]);
}
return getVars;
}
</script>
</head>
<body>
<script>
var g = parseGetVars();
for (var i in g)
document.writeln(g[i].replace(/\+/g,' ').replace(/\undefined/g,'')+'<br>');
</script>
</body>
</html>
[END CODE]

I don't know if this is the most elegant way to do this, but I just can't get the values to display without either an undefined or blank entry. I need some sort of 'if (Address3 == null)' skip to next value?

Pete.
Reply With Quote
  #4 (permalink)  
Old May 24th, 2006, 09:14
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Form value shows as Undefined

I'm still not entirely clear what exactly you are trying to do. Are you able to post links to your page so that we can see the full story? Is the idea that the end user fills in the details, submits the form and the details come to you in the form of an email or are you trying to retrieve someones details from, for example, a database and display them on the page?
Reply With Quote
  #5 (permalink)  
Old May 24th, 2006, 09:35
New Member
Join Date: May 2006
Location: Suffolk, United Kingdom
Age: 47
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Form value shows as Undefined

It's nothing quite so advanced. The ultimate aim is to produce a printable order form for a book by getting the visitor to input their details to a form on the web site (Not yet live). This form will validate the content and make sure they don't leave any essential information out. When this gets submitted, a new page should appear that is set out in printer friendly format - showing the name and address info and order information etc...

I just want the name and address portion to display without blanks or the word "undefined" appearing when the visitor has not got a 3rd address line as it is optional. I would like the name and address info to appear in just 1 column.

Pete.
Reply With Quote
  #6 (permalink)  
Old May 24th, 2006, 10:40
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Form value shows as Undefined

Give your fields id's and use css to hide unwanted fields. The processing would be something like:
Code: Select all
if (document.getElementById('address3').value == ''){ document.getElementById('address3').style.display = 'none'; }
You obviously need to incorporate this into a function and you could extend it to check for any allowed missing values.
Reply With Quote
  #7 (permalink)  
Old May 24th, 2006, 11:53
New Member
Join Date: May 2006
Location: Suffolk, United Kingdom
Age: 47
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Form value shows as Undefined

Ok. Sorry to be a dummy, but on which page do I do this, the form or the receiving page? How do I create a function for this and where do I call it from?

Pete.
Reply With Quote
  #8 (permalink)  
Old May 24th, 2006, 13:07
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Form value shows as Undefined

Quote:
Originally Posted by peter_day85
Ok. Sorry to be a dummy, but on which page do I do this, the form or the receiving page? How do I create a function for this and where do I call it from?
That depends. By what process is the information entered on the form getting to the receiving page?
Reply With Quote
  #9 (permalink)  
Old May 24th, 2006, 13:39
New Member
Join Date: May 2006
Location: Suffolk, United Kingdom
Age: 47
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Form value shows as Undefined

The form is submitted with a GET method.

Code: Select all
<HTML>
<HEAD>
<TITLE>Required Select Field</TITLE>
<script Language="JavaScript">
function validate() {
if (MainForm.County.options[0].selected) {
alert('Please choose a County.');
MainForm.County.focus();
event.returnValue=false;
}
if (MainForm.Address1.value == '') {
alert('Please enter your House Number or Name.');
MainForm.Address1.focus();
event.returnValue=false;
}
if (MainForm.Postcode.value == '') {
alert('Please enter a Postcode.');
MainForm.Postcode.focus();
event.returnValue=false;
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="MainForm" ACTION="H:\desktopxp\printform.htm" METHOD="GET" onsubmit="validate();">
<B>Full Name: </B><INPUT TYPE=TEXT NAME="FullName" SIZE=20>
<P>
<B>Address 1: </B><INPUT TYPE=TEXT NAME="Address1" SIZE=30>
<P>
<B>Address 2: </B><INPUT TYPE=TEXT NAME="Address2" SIZE=30>
<P>
<B>Address 3: </B><INPUT TYPE=TEXT NAME="Address3" CLASS="optional" SIZE=30>
<P>
<B>Town or City: </B><INPUT TYPE=TEXT NAME="Town" SIZE=20>
<P>
<B>County: </B><SELECT NAME="County">
<OPTION VALUE="0" SELECTED>Select County</OPTION>
<OPTION VALUE="Suffolk">Suffolk</OPTION>
<OPTION VALUE="Norfolk">Norfolk</OPTION>
<OPTION VALUE="West Sussex">West Sussex</OPTION>
<OPTION VALUE="Kent">Kent</OPTION>
<OPTION VALUE="Surrey">Surrey</OPTION>
</SELECT>
<P>
<B>Postcode: </B><INPUT TYPE=TEXT NAME="Postcode" SIZE=10>
<P>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>
There's a lot to complete obviously, but I just want to get this working before I go further.

Pete.
Reply With Quote
  #10 (permalink)  
Old May 24th, 2006, 21:24
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: Form value shows as Undefined

Looking at what you have provided, the job needs to be taken care of in 'printform.htm'.

But there are lots of things you need to take care of:

Code you file to some DTD standard.
Use only lowercase letters for your tag names, etc.
Don't use GET to submit form information as it shows up in the address bar.
Use POST.

I'm still not sure where your going with all this but it looks like you need to consider some form of server side coding.
Reply With Quote
Reply

Tags
form, value, shows, undefined

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined Index error with DB Sphinx111 PHP Forum 3 Apr 21st, 2008 20:37
Undefined index problem andrewlondon PHP Forum 12 Sep 2nd, 2007 12:19
Another with IE woes - Element is undefined weasel Web Page Design 3 Jul 5th, 2007 18:18
Undefined variable: row csun PHP Forum 3 Jun 5th, 2007 12:44
Undefined index using $GET_ LostProphet PHP Forum 7 Aug 22nd, 2006 11:15


All times are GMT. The time now is 22:50.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43