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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating hidden fields in form 1

Status
Not open for further replies.

rmagan

Programmer
Jun 3, 2003
35
US
I have a form that accepts a radio field and a select (multiple) box field and processes a function. When the user hits the go, I need to populate some hidden fields (p_hidden_period) based on the values of the radio and select fields. What is the best way to do this? Here is my code:

<html>
<form name=&quot;maint_student_schedule&quot; action=&quot;stars3.star_portal.process_student_schedule&quot; method=&quot;post&quot;>
<table ALIGN=&quot;LEFT&quot;><br><tr>
<td ALIGN=&quot;CENTER&quot;> <font CLASS=&quot;PortletText1&quot;>
<input type=&quot;radio&quot; NAME=&quot;p_sched_type&quot; id=&quot;sched_type1&quot; VALUE=&quot;ADD&quot; Checked> Add &nbsp;&nbsp
</td>
<td ALIGN=&quot;CENTER&quot;> <font CLASS=&quot;PortletText1&quot;>
<input type=&quot;radio&quot; NAME=&quot;p_sched_type&quot; id=&quot;sched_type2&quot; VALUE=&quot;DEL&quot; > Drop &nbsp;&nbsp
</td>
<td ALIGN=&quot;RIGHT&quot;> <font CLASS=&quot;PortletText1&quot;>
<input type=&quot;radio&quot; NAME=&quot;p_sched_type&quot; id=&quot;sched_type3&quot; VALUE=&quot;CHG&quot; > Change &nbsp; &nbsp
</td>
<td ALIGN=&quot;RIGHT&quot;> <font CLASS=&quot;PortletText1&quot;>
<select name=&quot;p_filter_period&quot; id=&quot;sched_choice1&quot; SIZE=&quot;3&quot; MULTIPLE>
<option value=01>01 &nbsp;&nbsp (07:00-08:44)
<option value=02>02 &nbsp;&nbsp (08:45-09:25)
<option value=03>03 &nbsp;&nbsp (09:25-10:10)
</select>
<input type=&quot;hidden&quot; name=&quot;p_hidden_period&quot; value=&quot;ALL&quot;>
</td>
<td ALIGN=&quot;CENTER&quot;><input type=&quot;submit&quot; name=&quot;p_action&quot; value=&quot;Go&quot;>
</td>
</tr>
</table>
</form>
</html>
 
First create a javascript function for populating the fields:

function fillFields(form){
// do your javascript checks here
form.hidden_field_name1.value=&quot;something&quot;;
form.hidden_field_name2.value=&quot;something&quot;;
form.hidden_field_name3.value=&quot;something&quot;;
form.hidden_field_name4.value=&quot;something&quot;;
}

Then inside the form tag add this:

onSubmit=&quot;fillFields(this)&quot;
 
rmagan,

Here's some code that should do the job for you:

Code:
<html>
<head>
<script language=&quot;javascript&quot;>
<!--

	function populateHidden()
	{		
		var formObj = document.forms['maint_student_schedule'];

 		for (var loopVar=0; loopVar<formObj.p_sched_type.length; loopVar++)
			if (formObj.p_sched_type[loopVar].checked)
				formObj.hidden01.value = &quot;You selected radio button: &quot; + formObj.p_sched_type[loopVar].value;

		var tempStr = &quot;&quot;;
 		for (var loopVar=0; loopVar<formObj.p_filter_period.options.length; loopVar++)
			if (formObj.p_filter_period.options[loopVar].selected)
			{
				if (tempStr != &quot;&quot;) tempStr += &quot;,&quot;;
				tempStr += &quot;&quot; + formObj.p_filter_period.options[loopVar].value;
			}
			formObj.hidden02.value = &quot;You selected items: &quot; + tempStr;

		alert(formObj.hidden01.value);
		alert(formObj.hidden02.value);
		// formObj.submit();
	}

//-->
</script>
</head>
</body>

<form name=&quot;maint_student_schedule&quot; action=&quot;stars3.star_portal.process_student_schedule&quot; method=&quot;post&quot;>
<input type=&quot;hidden&quot; name=&quot;hidden01&quot; value=&quot;&quot;>
<input type=&quot;hidden&quot; name=&quot;hidden02&quot; value=&quot;&quot;>
<table ALIGN=&quot;LEFT&quot;>
<tr>
	<td ALIGN=&quot;CENTER&quot;><input type=&quot;radio&quot; NAME=&quot;p_sched_type&quot; id=&quot;sched_type1&quot; VALUE=&quot;ADD&quot;  Checked> Add </td>
	<td ALIGN=&quot;CENTER&quot;><input type=&quot;radio&quot; NAME=&quot;p_sched_type&quot; id=&quot;sched_type2&quot; VALUE=&quot;DEL&quot; > Drop </td>
	<td ALIGN=&quot;RIGHT&quot;><input type=&quot;radio&quot; NAME=&quot;p_sched_type&quot; id=&quot;sched_type3&quot; VALUE=&quot;CHG&quot; > Change </td>
	<td ALIGN=&quot;RIGHT&quot;>
		<select name=&quot;p_filter_period&quot; id=&quot;sched_choice1&quot; SIZE=&quot;3&quot; MULTIPLE>
			<option value=01>01    (07:00-08:44)
			<option value=02>02    (08:45-09:25)
			<option value=03>03    (09:25-10:10)
		</select>
		<input type=&quot;hidden&quot; name=&quot;p_hidden_period&quot; value=&quot;ALL&quot;>
	</td>
	<td ALIGN=&quot;CENTER&quot;><input type=&quot;button&quot; name=&quot;p_action&quot; value=&quot;Go&quot; onClick=&quot;populateHidden();&quot;></td>
</tr>
</table>
</form>

</body>
</html>

At the moment, I alert what would be populated in the hidden fields... But if you uncomment line 25, and then remove the two alerts, the form will be submitted as normal.

If you want, you can even put some validation routines in before the submit.

Hope this helps!

Dan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top