Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a function through a button -> submitting a forum 3

Status
Not open for further replies.

fdarkness

Programmer
Feb 17, 2006
110
CA
Please bear with me... this may get a bit convoluted.

I have a form where I need to replace some data in it. I'm doing this through a javascript function which is launched through an onClick event in a button input type. Once the data is run through the function, I need the page to submit itself and then do stuff to the form data.

I'm running into a problem with getting the page to submit. The function runs properly with the onClick in the button, but after that, it either does nothing or goes to the next stage but wasn't properly submitted as the form items didn't get passed through. Here's the important pieces of code, stored on the page warrantEdit2.asp which is where the submit is directed to as well (if statements directed by the parameters in the URL deal with how to proceed):

Code:
<SCRIPT LANGUAGE="JavaScript">
function testResults (form) 
{
	for(i = 0; i < document.forms[0].elements.length; i++)
	{
		alert(document.forms[0].elements[i].value.replace(/,/, "&#044;"))
	}
	document.forms[0].submit
}
</SCRIPT>


<form name="tracingEdit" action="warrantEdit2.asp?Action=Tracing&Function=submit" method="post">
Elements and stuff

<input type="button" name="submit" value="Submit Changes" onClick="testResults(this.form)">
</form>

So far, I've tried:
* document.forms[0].submit (doesn't do anything)
* document.tracingEdit.submit (doesn't do anything)
* return true (doesn't do anything)
* window.location='warrantEdit2.asp?Action=Tracing&Function=submit' (works, but isn't a proper submit and data from the form elements are lost)

Any ideas on how I can get this to submit properly?
 
why not call your function from the form's onsubmit event?

Code:
<form name="tracingEdit" action="warrantEdit2.asp?Action=Tracing&Function=submit" method="post" [red]onclick="return testResults(this);"[/red]>
Elements and stuff

<input type="[red]submit[/red]" name="[red]btnSubmit[/red]" value="Submit Changes" [red]/[/red]>
</form>

as an aside, you're not calling the submit event - you need to include the parentheses...

Code:
document.forms[0].submit[red]()[/red]

but that line can be completely removed if you go with my method described above.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
cLFlaVA: that worked great, except now I'm running back into the problem I was trying to solve in the first place! (except now I do a bunch of things first before having the problem crop up! :)

Is it possible to assign the contents of the input elements *back* to themselves after running a function on it? In this case, I'm running replace on the form elements to change the comma (,) to &#044; as my VBScript / ASP code is working with comma deliminated strings which the input mucks up if a comma is entered.

I tried
Code:
document.forms[0].elements[i].value = (document.forms[0].elements[i].value.replace(/,/, "&#044;"))

but it doesn't seem to be working. I'm winding up with my VBScript function breaking up my text entry in the wrong spots.
 
You'll probably want to escape the commas in the regexp - and you also need to give the global parameter to the regexp to ensure that it replaces ALL the commas instead of just the first.

Here's a test to see how it works:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>Millennium Software, Inc. - Homepage</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function removeCommas(obj) {
   obj.value = obj.value.replace(/\,/g, "&#044;");
}

</script>
</head>
<body>
<input type="text" id="blah" />
<input type="button" value="replace commas" onclick="removeCommas(document.getElementById('blah'))" />
</body>
</html>

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
if I understand correctly, you could write this in the function:

Code:
elementsOrigVal=new Array();
for(i = 0; i < document.forms[0].elements.length; i++)
{
      elementsOrigVal[i]=document.forms[0].elements[i].value;
      document.forms[0].elements[i].value = (document.forms[0].elements[i].value.replace(/,/, "&#044;"));
}

And to restore the values:
Code:
for(i = 0; i < document.forms[0].elements.length; i++)
{
      document.forms[0].elements[i].value=elementsOrigVal[i];
}

Hope this helps :)
 
cLFlaVA: Yes, the sample I showed wasn't doing anything to the values but displaying them. My mistake... what I need to do is take each element, replace the commas in the text, then submit the values, hence the following line of code that I tried:

Code:
document.forms[0].elements[i].value = (document.forms[0].elements[i].value.replace(/,/, "&#044;"))

which doesn't seem to work.

kaht: I'll put in the g parameter, but the replace seems to work... I was displaying the results in an alert pop-up first to make sure. It just doesn't seem to be assigning the altered string *back* to the form elements. Maybe this isn't possible?

bigS47: I don't need to store the original... just replace the text from the input elements with the values in the replace function. I'm then processing the text through VBScript that's using a function to break up the comma-deliminated string that gets passed to it. It works fine, except when there's a comma within the text.
 
Oh my hat. Nevermind. It seems my assignment was working fine... I just had a syntax error in the OnClick which wasn't even launching the damn function. *grr*.

It works great! Thanks guys!!
 
Cory's got my bizzack in this hizzay, peace. [turkey]

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top