Forum Moderators: open

Message Too Old, No Replies

Dynamic Form Paramters

onsubmit and action

         

timswrks

10:48 pm on Apr 8, 2010 (gmt 0)

10+ Year Member



Hello-

Could use some help with some form scripts.

I have a page/form which will be sent to pages depending on which checkboxes are checked. Also, there is a script which validates the form when the submit button is clicked, using the onsubmit parameter of the form tag. The action and onsubmit parameters are dynamically written via a javascript.

The html initially sets up the page without input text boxes and a disabled submit button. When the checkbox is clicked and onclick in the tag calls functions that add an appropriately named text box (via innerHtml) and activates the Submit Button. The reverse is true if boxes are unchecked. The script is also to modify the action and onsubmit parameters of the form tag.

The page is not working correctly. In both FireFox and IE8, the action parameter is correctly dynamically written when a checkbox is checked and the link correctly works when the submit button is clicked. However, the fnChkItms function, called by the 'onsubmit' parameter does not run and the link executes without validation.

In FF, using Firebug, the form tag does not have an 'onsubmit' parameter written after a checkbox is checked. In IE8, using Developer Tools, the onsubmit parameter is correctly written, but apparently does not fire.

The page is listed below. The HTML commented out is a hard wired version of the dynamic page, but having only one checkbox line. It works correctly with respect to both action and onsubmit parameters.

Am I missing something, or is the onsubmit parameter not dynamically writable?

TIA for any thoughts and help.


PS--> Note that prmForm.elements[i].checked is using no i instead of including the i because I was getting an error in the post with correct array format. Just add the i back to array to test. I'm new here and don't know how to correct for posting.

<html>
<head>

<script language="JavaScript">
<!--

var bolIsSent = false;

function fnWrite(prmObj, prmForm)
{

var strObjID = prmObj.value;
var intObjID = parseInt(prmObj.value);

document.getElementById("sbtArtDta").disabled = true;

if(prmObj.checked)
{
document.getElementById("nfoCntnr_" + strObjID).innerHTML =
"<input type='text' id='TEXT_" + strObjID + "' name='txt" + strObjID + "'>";
}
else
{
document.getElementById("nfoCntnr_" + strObjID).innerHTML = "&nbsp;";
}


for (var i=0; i < prmForm.elements.length; i++)
{
if (prmForm.elements i.checked)
{
alert("In OnSubmit, elmnt " + i);

prmForm.onsubmit="return fnChkItms(this)";
prmForm.action="tstWriteTblProc.asp";

break;
}
}


for (var i=0; i < prmForm.elements.length; i++)
{

//re-insert i into array below
if (prmForm.elements[].checked)
{
document.getElementById("sbtArtDta").disabled = false;

break;
}
}
}

function fnChkItms(prmForm)
{
alert("In ChkItms");

if(!bolIsSent)
{
bolIsSent = confirm("The " + prmForm.id + " Will be Submitted");
return (bolIsSent);
}
else
{
return (false);
}
}

-->
</script>



</head>
<body>



<table width="700" align="center" border="1" cellspacing="1" cellpadding="1">

<form method='POST' id='ClntObj_Ordr' name='frmArt' >
<tr>
<td width="150">
Art Type 01
</td>
<td width="15" align="center">
<input type="checkbox" id="checkbox1" name="checkbox1" value="01" onClick="fnWrite(this, this.form)">
</td>
<td align="center" width="530" id="nfoCntnr_01" name="nfoCntnr_01">
&nbsp;
</td>
</tr>

<tr>
<td width="150">
Art Type 02
</td>
<td width="15" align="center">
<input type="checkbox" id="checkbox2" name="checkbox2" value="02" onClick="fnWrite(this, this.form)">
</td>
<td align="center" width="530" id="nfoCntnr_02" name="nfoCntnr_02">
&nbsp;
</td>
</tr>

<tr>
<td width="150">
Art Type 03
</td>
<td width="15" align="center">
<input type="checkbox" id="checkbox3" name="checkbox3" value="03" onClick="fnWrite(this, this.form)">
</td>
<td align="center" width="530" id="nfoCntnr_03" name="nfoCntnr_03">
&nbsp;
</td>
</tr>

<tr>
<td colspan="3">
&nbsp;
</td>
</tr>
<tr>
<td align="center" colspan="3" id="SbtBtn" name="SbtBtn">
<input type='submit' value='Submit Art Data' id='sbtArtDta' name='sbtArtDta' disabled='true'>
</td>
</tr>
</form>
</table>

<!--

<table width="700" align="center" border="1" cellspacing="1" cellpadding="1">
<form action='tstWriteTblProc.asp' method='POST' id='frmArt' name='frmArt' onsubmit="return fnChkItms(this)">
<tr>
<td width="150">
Art Type 0X
</td>
<td width="15" align="center">
<INPUT type="checkbox" id=checkbox4 name=checkbox4>
</td>
<td align="center" width="530" id="nfoCntnr_01" name="nfoCntnr_01">
<input type='text' id='TEXT_0X' name='txt0X'>
</td>
<tr>
<td colspan="3">
&nbsp;
</td>
</tr>
<tr>
<td align="center" colspan="3">
<input type='submit' value='Submit Art Data' id='sbtArtDta' name='sbtArtDta'>
</td>
</tr>
</form>
</table>

-->

</body>
</html>

Fotiman

1:14 pm on Apr 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!

First, the cleanup... these items are not related to your problem, but should be cleaned up anyway.

1. Don't use the language attribute (it's not valid), use the type attribute:
<script language="JavaScript">
should be:
<script type="text/javascript">

2. Don't put HTML comments within the script element. This was needed for some 1.0 browser versions and hasn't been needed for many, many years.
<script ...>
<!--
...
-->
</script>

Should just be:
<script ...>
...
</script>

Now for the more serious problems.

Your HTML is invalid. The TABLE element can not directly contain a FORM element. Start by validating your markup [validator.w3.org]. Then move your FORM element to be on the outside of your TABLE:
<form ...>
<table ...>
...
</table>
</form>


Next, lets look at this code:

for (var i=0; i < prmForm.elements.length; i++)
{
if (prmForm.elements i.checked) {
alert("In OnSubmit, elmnt " + i);
prmForm.onsubmit="return fnChkItms(this)";
prmForm.action="tstWriteTblProc.asp";
break;
}
}


prmForm.onsubmit="return fnChkItms(this)";

That example is assigning a string value to your onsubmit handler. What you want to do is assign a function. Try this instead:

prmForm.onsubmit = function () {
return fnChkItms(prmForm);
}


Give that a try and see if it works. :)

timswrks

10:44 pm on Apr 9, 2010 (gmt 0)

10+ Year Member



many, many, many thanks for the format and code help. Much hair-pulling now ended.