View Full Version : Javascript function to add members rearranging array list when entered

06-17-2009, 08:24 PM
I have a form that is done in php I'm converting it to .NET but the java script has an error. I posted all the code but the error is in the "saveOtherSurvivorValues" function i think. What happens is when you click the "add survivor" link to add a the first survivor and then fill out the information for the first survivor then click add survivor a second time for a second survivor it moves the data from the 1st survivor into different fields to produce in accurate data. It has me stumped help would be greatly appreciated.

Here is a link to the live form.


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class familyContact : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
Label lblPageTitle = (Label)this.Master.FindControl("lblEventTitle");
lblPageTitle.Text = "Family Contact Form";
if (IsPostBack == false)
dataaccess clsDataaccess = new dataaccess();
clsDataaccess.LoadDropDown(this.dropincidentDescription, "DeathCode", "DeathCode", "DeathDescrip", "DeathDescrip");
clsDataaccess.LoadDropDown(this.dropincidentWeapon, "WeaponList", "WeaponID", "WeaponDescr", "WeaponDescr");

protected void btnSubmit_Click(object sender, EventArgs e)

public void SaveOfficerContact()
int intFormInfoID = 0;
dataaccess clsDataAccess = new dataaccess();

SqlConnection conn = clsDataAccess.GetConn();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "AddFormInfo_Officer";

cmd.Parameters.Add(new SqlParameter("@OffFirstName", txtoffericerFirstName.Text));
cmd.Parameters.Add(new SqlParameter("@OffMidName", txtofficerMiddleName.Text));
cmd.Parameters.Add(new SqlParameter("@OffLastName", txtofficerLastName.Text));
cmd.Parameters.Add(new SqlParameter("@OffSuffix", txtofficerSuffix.Text));
cmd.Parameters.Add(new SqlParameter("@OffRank", txtofficersrank.Text));
cmd.Parameters.Add(new SqlParameter("@OffDateOfincident", txtdateOfIncident.Text));
cmd.Parameters.Add(new SqlParameter("@OffDateOfDeath", txtdateofDeath.Text));
cmd.Parameters.Add(new SqlParameter("@ContTitle", txtliaisonOfficertitle.Text));
cmd.Parameters.Add(new SqlParameter("@ContFName", txtliaisonOfficerFirstName.Text));
cmd.Parameters.Add(new SqlParameter("@ContLName", txtliaisonOfficerLastName.Text));
cmd.Parameters.Add(new SqlParameter("@ContStr", txtliaisonOfficerstreet.Text));
cmd.Parameters.Add(new SqlParameter("@ContStr2", txtliaisonOfficerstreet2.Text));
cmd.Parameters.Add(new SqlParameter("@ContCity", txtliaisonOfficerCity.Text));
cmd.Parameters.Add(new SqlParameter("@ContState", txtliaisonOfficerState.Text));
cmd.Parameters.Add(new SqlParameter("@ContZip", txtliaisonOfficerZip.Text));
cmd.Parameters.Add(new SqlParameter("@ContPh", txtliaisonOfficerPhoneNumber.Text));
cmd.Parameters.Add(new SqlParameter("@ContExt", txtliaisonOfficerPhoneExtension.Text));
cmd.Parameters.Add(new SqlParameter("@ContAltPh", txtliaisonOfficerPhoneNumber2.Text));
cmd.Parameters.Add(new SqlParameter("@ContEmail", txtliaisonOfficerEmail.Text));
cmd.Parameters.Add(new SqlParameter("@TypeOfDeath", dropincidentType.SelectedValue));
cmd.Parameters.Add(new SqlParameter("@DeathCode", dropincidentDescription.SelectedValue));
cmd.Parameters.Add(new SqlParameter("@Flag", dropofficerDuty.SelectedValue));
cmd.Parameters.Add(new SqlParameter("@Circum", txtincidentDescribe.Text));
cmd.Parameters.Add(new SqlParameter("@WeaponListID", dropincidentWeapon.SelectedValue));
cmd.Parameters.Add(new SqlParameter("@Vest", dropincidentArmor.SelectedValue));
cmd.Parameters.Add(new SqlParameter("@VestDesc", txtincidentEffective.Text));
cmd.Parameters.Add(new SqlParameter("@AlcoholRelated", dropincidentAlcohol.SelectedValue));
cmd.Parameters.Add(new SqlParameter("@DrugRelated", dropincidentDrug.SelectedValue));
cmd.Parameters.Add(new SqlParameter("@DrugRelatedDesc", txtincidentExplain.Text));
cmd.Parameters.Add(new SqlParameter("@AllegedPerps", txtincidentAgeandGender.Text));
cmd.Parameters.Add(new SqlParameter("@TrafficStop", dropincidentTraffic.SelectedValue));

cmd.Parameters.Add(new SqlParameter("@SubmitterFirstName", txtcompleteFirstName.Text));
cmd.Parameters.Add(new SqlParameter("@SubmitterLastName", txtcompleteLastName.Text));
cmd.Parameters.Add(new SqlParameter("@SubmitterEmail", txtcompleteEmailAddress.Text));
cmd.Parameters.Add(new SqlParameter("@SubmitterPhone", txtcompletePhoneNumber.Text));
cmd.Parameters.Add(new SqlParameter("@FormInfo_OfficerID", intFormInfoID));


Philip M
06-17-2009, 08:30 PM
This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

06-17-2009, 08:53 PM
Would you know of a good place to post a problem like this?

Old Pedant
06-17-2009, 11:59 PM
Philip obviously did *NOT* look at your page.

Philip: This *IS* a JavaScript problem.

He clicks on this:

<p><a href="javascript:addOtherSurvivor();">Add Survivor</a></p>

and it does the wrong thing. Patently JavaScript.

Hennignfeld: You just confused the issue by mentioning your PHP-to-ASP.Net conversion. Obviously irrelevant to the JavaScript problem.

Back in a bit.

Old Pedant
06-18-2009, 12:15 AM
Ugh. That is some REALLY UGLY JavaScript code!

There's no reason at all to have written the code like that.

It saves all the values from the existing survivors into an array and then tries to regenerate the entire list of survivors, adding a new blank set of input fields to the form.


Why save and restore???

If done right, using document.CreateElement( ) calls and appendXXX calls, you would just leave the elements that are already there in place and add the new blank one.

Really, this code BADLY needs to be rewritten. Nothing to do with PHP or ASP.NET. Just bad JavaScript code.

No, I don't see why the code doesn't restore the values properly, when it pumps them back out again. It really messes up a DELETE badly, too.


I should note, in passing, that the ".NET-ish" way of doing this would be to have a post-back to the server and have the server rewrite the <form> with the added fields. If you used Visual Studio to re-create this page from scratch, that's what it would probably try to help you do. ASP.NET doesn't do a good job of integrating server-side code with JavaScript actions; if you want heavy-weight JS code, you pretty much have to do it on your own. For instance, with this JS code (even if it worked), the ASP.NET code would *not* be aware of the added form fields and you'd have to use what ASP.NET considers "primitive" coding to get their values.

So before you go to the work of fixing that JS code, you need to decide if you are going to REALLY use ASP.NET and do things the .NET-ish way or you are going to just marginally use ASP.NET and instead do things the PHP way with ASP.NET syntax.

Old Pedant
06-18-2009, 12:23 AM

I actually found the source of your bugs!

At least one source. May be others!

function saveOtherSurvivorValues()
for(var i = 0; i < arrOtherSurvivor.length; i++)
arrOtherSurvivor[i][0] = document.getElementById("OtherSurvivorCode" + i).value;
arrOtherSurvivor[i][1] = document.getElementById("OtherSurvivorTitle" + i).value;
arrOtherSurvivor[i][1] = document.getElementById("OtherSurvivorFirstName" + i).value;
arrOtherSurvivor[i][2] = document.getElementById("OtherSurvivorMiddleName" + i).value;
arrOtherSurvivor[i][3] = document.getElementById("OtherSurvivorLastName" + i).value;
arrOtherSurvivor[i][1] = document.getElementById("OtherSurvivorSuffix" + i).value;
arrOtherSurvivor[i][4] = document.getElementById("OtherSurvivorDateOfBirth" + i).value;
arrOtherSurvivor[i][5] = document.getElementById("OtherSurvivorGuardian" + i).value;
arrOtherSurvivor[i][6] = document.getElementById("OtherSurvivorLocation" + i).value;
arrOtherSurvivor[i][7] = document.getElementById("OtherSurvivorStreet" + i).value;
arrOtherSurvivor[i][8] = document.getElementById("OtherSurvivorStreetTwo" + i).value;
arrOtherSurvivor[i][9] = document.getElementById("OtherSurvivorCity" + i).value;
arrOtherSurvivor[i][10] = document.getElementById("OtherSurvivorState" + i).value;
arrOtherSurvivor[i][11] = document.getElementById("OtherSurvivorZip" + i).value;
//arrOtherSurvivor[i][12] = document.getElementById("OtherSurvivorPhone" + i).value;
//arrOtherSurvivor[i][13] = document.getElementById("OtherSurvivorPhoneType" + i).value;

So your numbers there are all mixed up.

This would not have happened if the code used an array of JS objects instead of an array of arrays. Ugh.

Do you want a JS re-write, or are you going to fix it by using ASP.NET-ish coding, instead?

Old Pedant
06-18-2009, 12:27 AM
If it's not obvious, that code should be:

for(var i = 0; i < arrOtherSurvivor.length; i++)
arrOtherSurvivor[i][0] = document.getElementById("OtherSurvivorCode" + i).value;
arrOtherSurvivor[i][1] = document.getElementById("OtherSurvivorTitle" + i).value;
arrOtherSurvivor[i][2] = document.getElementById("OtherSurvivorFirstName" + i).value;
arrOtherSurvivor[i][3] = document.getElementById("OtherSurvivorMiddleName" + i).value;
arrOtherSurvivor[i][4] = document.getElementById("OtherSurvivorLastName" + i).value;
arrOtherSurvivor[i][5] = document.getElementById("OtherSurvivorSuffix" + i).value;
arrOtherSurvivor[i][6] = document.getElementById("OtherSurvivorDateOfBirth" + i).value;
arrOtherSurvivor[i][7] = document.getElementById("OtherSurvivorGuardian" + i).value;
arrOtherSurvivor[i][8] = document.getElementById("OtherSurvivorLocation" + i).value;
arrOtherSurvivor[i][9] = document.getElementById("OtherSurvivorStreet" + i).value;
arrOtherSurvivor[i][10] = document.getElementById("OtherSurvivorStreetTwo" + i).value;
arrOtherSurvivor[i][11] = document.getElementById("OtherSurvivorCity" + i).value;
arrOtherSurvivor[i][12] = document.getElementById("OtherSurvivorState" + i).value;
arrOtherSurvivor[i][13] = document.getElementById("OtherSurvivorZip" + i).value;
//arrOtherSurvivor[i][14] = document.getElementById("OtherSurvivorPhone" + i).value;
//arrOtherSurvivor[i][15] = document.getElementById("OtherSurvivorPhoneType" + i).value;

But even if you stick with this methodology I'd rewrite this stuff to make it more stable and more readable.


06-18-2009, 01:20 PM
Thank you i will test it. I know the code is shady unfortunately I'm not the person who coded it this way, so it was hard to follow.