Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 10th, 2004, 07:40 PM
Authorized User
 
Join Date: Jun 2003
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error in form validation using "this" keyword

Greetings,

I am having a yuk debugging problem. My ego is deflated.

I am trying to pass variables into a validation function (client-side) from multiple locations in my web site, so I want to use the "this" keyword from each form.

I have condensed the code into as small a chunk as possible as follows. (The loose doctype was a desperate gasp.):


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TRhtml4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="JavaScript" type="text/javascript">

function validateitem(objForm){
    returnValue=false;
    document.write("form element 'amount': " +objForm.amount.value+ "<br>\r");
    document.write("form element 'item_name': " +objForm.item_name.value+ "<br>\r");
    document.write("form element 'business': " +objForm.business.value+ "<br>\r");
    return returnValue;
    }
</script>

</head>
<body>

<form name="form1"
action="https://www.paypal.com/cgi-bin/webscr"
method="post"
onsubmit="return validateitem(this);">
<input type=hidden name="cmd" value="_cart">
<input type=hidden name="business" value="[email protected]">
<input type=hidden name="item_name" value="Bulgarian Lavender">
<input type=hidden name="amount" value="12">
<input type=submit name="submit" value="Submit" alt="Make payments with PayPal - it's fast, free and secure!">
</form>
</body>
</html>



Pressing the submit button produces the following error:

"'item_name.value' is null or not an object."

If I delete the .value as follows:

objForm.amount
objForm.item_name

then these values are returned as "undefined."


Ironically, If I switch the order of statements in the function as follows:

function validateitem(objForm){
    returnValue=false;
    document.write("form element 'item_name': " +objForm.item_name.value+ "<br>\r");
    document.write("form element 'amount': " +objForm.amount.value+ "<br>\r");
    document.write("form element 'business': " +objForm.business.value+ "<br>\r");
    return returnValue;
    }

then whichever write statement is written first, will correctly execute with the correct output, and the latter statements will be undefined.

Thank you very much for your time.
Daniel Hutchins

Thank you,
Daniel Hutchins
Woodbridge, CA
http://www.finehomemadesoap.com
__________________
Daniel Hutchins
Stockton, CA
http://www.finehomemadesoap.com
 
Old January 10th, 2004, 07:49 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Daniel,

I think that you must assign IDs to the inputs that you refer to. However, perhaps this is not the problem, having the first one in the list work fine.

Maybe you should try:

<input type=hidden id="item_name">

etc.

Sorry if this isn't the problem, just a guess.

Snib
 
Old January 10th, 2004, 11:12 PM
Authorized User
 
Join Date: Jun 2003
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Snib, but guess what....

Using id="item_name" I still get the error message "'item_name.value' is null or not an object."


Everybody passes variables into a validation function. There has got to be a simple mistake in here somewhere.


Further debugging information: I used typeof(objForm) to make sure it is an object (hopefully the forms object) and objForm.elements.length

to determine how many elements it has. The code acknowledges that I have an object, but with zero elements. The output (which is written

before the error message) is as follows:

type: object
number of elements: 0
name of form: form1
[followed by the error message "'item_name.value' is null or not an object."]


for the following code:

function validateitem(objForm){
    var returnValue=false;

    var x;

    x = typeof(objForm);
    document.write("type: " +x+ "<br>\r");

    x = objForm.elements.length;
    document.write("number of elements: " +x+ "<br>\r");

    x = objForm.name;
    document.write("name of form: " +x+ "<br>\r");

    x = objForm.item_name.value;
    document.write("form element item_name: " +x+ "<br>\r");

    return returnValue;
}
</script>


<form action="https://www.paypal.com/cgi-bin/webscr"
        name="form1"
        method="post"
        onsubmit="return validateitem(this)">
    <input type=hidden id="item_name" name="item_name" value="Bulgarian Lavender">
</form>

I have tried passing the form using onsubmit="return validateitem(document.form1)", and I still got the same problem in the validation

function.


Thank you,
Daniel Hutchins
Woodbridge, CA
http://www.finehomemadesoap.com
 
Old January 11th, 2004, 02:56 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I can't see anything obvious. One thing though is to rename your submit button to something other than 'submit'. You are confusing the issue because form.submit method is now over-ridden by form.submit element.


--

Joe
 
Old January 12th, 2004, 08:22 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Its just the use of document.write that's causing the problems. As soon as you do a document.write you're clearing out the existing document, so the form values are no longer valid. Change each document.write to an alert and you'll see it works just fine.
 
Old January 12th, 2004, 06:22 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

That explains why only the first line works. After the first line, the others no longer exist.

Snib

 
Old January 19th, 2004, 03:39 PM
Authorized User
 
Join Date: Jun 2003
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Solution to my own topic:

Everything was correct...except I ruined everything when I used "document.write()" in debugging.

Indeed document.write() did write to the current page, after erasing all data that was on it, including the form object.

Wow did I feel ridiculous when I discovered this truth. Oops.

regards,

Thank you,
Daniel Hutchins
Woodbridge, CA
http://www.finehomemadesoap.com
 
Old April 29th, 2004, 06:29 PM
Registered User
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

maybe someone can help me too - with passing the total and shipping prices into paypal please on www.candgcomputers.com
i am willing to pay!
thanks
barry
[email protected]

 
Old June 2nd, 2004, 05:00 PM
Registered User
 
Join Date: Apr 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

not to worry all completed
www.ezhost.co.uk






Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Form Validation Help jonsey Javascript 0 April 25th, 2007 08:14 AM
JavaScript form field validation error issue crmpicco Javascript How-To 4 July 4th, 2005 10:10 AM
Form Validation aware Javascript How-To 1 July 15th, 2004 02:44 PM
Form not refreshing after form validation Mimi Javascript How-To 0 August 25th, 2003 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.