Solved

function runs once, then error: "ïs not a function"

Posted on 2010-08-26
5
355 Views
Last Modified: 2013-11-11
Hi experts,

I have created a javascript function which posts form data for a single form, then goes trough a loop which posts multiple forms. Both these methods use the same function to actually post the data, but when i try to post with the loop it gives a javascript error: "post is not a function" and clicking on the error shows the line

post = post( action, parameters );

Open in new window


But when i called it once before the loop the function was succesfully executed. Very strange.

Below the code that calls the function. The first code shows the single and looped call to a function. The second part is the called function which in turn calls the 3rd function which posts the data using ajax.
// save the main form
result = saveForm( action, what, main );
	
// save the available forms
saveForms( 'address' );
saveForms( 'phone' );
saveForms( 'email' );

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

// the string with the POST data
parameters = 'what=company&id=' + generateId();
	
// add the field names and values to the post data
for( var i = 0; i < form.length; i++ )
{
	// save the form field
	field = form.elements[i];
		
	// add the field to the string
	parameters += '&' + field.name + '=' + field.value;
}

// add the company/contact id
parameters += '&c_id=' + c_id;
	
// post the data
post = post( action, parameters );
	
// return the value
return post;

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

// get the xmlHttp object
xmlHttp = getXmlHttpObject();
	
// open the page
xmlHttp.open( 'POST', '/modules/resources/add.php', false);
	
// set the request headers
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", parameters.length);
xmlHttp.setRequestHeader("Connection", "close");
	
// send the data
xmlHttp.send( parameters );
	
// return the text that's recieved from the server
return xmlHttp.responseText;

Open in new window

0
Comment
Question by:mverschoof
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33530140
use a different variable for post

<<      
// post the data
postvar = post( action, parameters );
      
// return the value
return postVar;
>>
0
 
LVL 5

Author Comment

by:mverschoof
ID: 33530267
I'll give it a try. Thanks for your quick reply!

Cheers, Michael
0
 
LVL 5

Author Comment

by:mverschoof
ID: 33530310
I changed both the post variables to a different name and it worked! I was pulling my hair out for two days unable to find it...

Sometimes the answer is so simple. It just needs a different perspective.

Well done!
0
 
LVL 5

Author Closing Comment

by:mverschoof
ID: 33530321
in the example given by gurvinder372, 'post' is replaced by 'postvar' and 'postVar'. These should both be the same... (for future viewer)
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33530345
Thanks for the points
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now