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

Pre-populated form fields problem

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
0
0
US
OK, I have a form that needs to be pre-populated with values from a database and then those values (or updated values if they have changed) need to be inserted back into the db. I can pull the data and write the data just fine, the problem comes in when I want to change the data to write it back to the db. It is a form to collect numbers for conferences, just numbers of boys and girls, very simple, so if the group numbers change, they can update their record. I have this code to calculate a total

Code:
	<script language="JavaScript" type="text/javascript">
	function calculate(el)
	{
		var form = el.form;
		var male_students = Number(form.elements["male_students"].value );
		var female_students = Number(form.elements["female_students"].value );
		var male_adults = Number(form.elements["male_adults"].value );
		var female_adults = Number(form.elements["female_adults"].value );
		var total = male_students + female_students + male_adults + female_adults;
		  
		// Check for user entry errors
		if (isNaN(total)) {
			// Alert user of error
			alert("Please enter only numbers");
		} else {
			// Show total in the form field, and select the form field text
			form.elements["total"].value = total;
			//form.elements["total"].select();
		}
	  
	}		
    </script>

and then this sample for a form field

Code:
<input name="male_students" type="text" id="male_students" size="3" value="<%=ms%>" onchange="calculate(this)">

The problem I am having is that the numbers update and recalculate correctly on the page, but when I submit the data or look at the source code after making the changes, it has the initial values so no changes are being made. What am I missing?

Thanks for any fresh eyes you can give to me!

WB
 
You should not see any changes in the source code due to user entry. The source code was written server side and is ancient history at that point. Client side javascript changes to the DOM or contents will not be reflected either.

You should be able to submit the entered values. How are you submitting the form? With a submit button or using javascript?

 
I am just using a submit button

Code:
<input type="hidden" name="status" value="submitted" ID="status">
<input type="image" name="submit" id="submit" src="[URL unfurl="true"]https://secure.ycmhome.org/images/submit_blue.gif"[/URL] onmouseover='this.src="[URL unfurl="true"]https://secure.ycmhome.org/images/submit_green.gif"';[/URL] onmouseout='this.src="[URL unfurl="true"]https://secure.ycmhome.org/images/submit_blue.gif"';[/URL] value="Submit &gt;&gt;" width="112" height="32">

I had wondered if the use of the javascript to calculate the total might do something to the form submission, but it all works great for the initial submission where everything starts out hardcoded to 0.
 
OK, nevermind. I was writing the new values to the db, I just realized that I wasn't updating the existing values, I was writing new values to a new record. Dang. Thanks for the help!

wb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top