Link to home
Start Free TrialLog in
Avatar of mverschoof
mverschoofFlag for Netherlands

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mverschoof

ASKER

I'll give it a try. Thanks for your quick reply!

Cheers, Michael
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!
in the example given by gurvinder372, 'post' is replaced by 'postvar' and 'postVar'. These should both be the same... (for future viewer)
Thanks for the points