Click to See Complete Forum and Search --> : Updating an Array


CodeNoob
12-10-2003, 09:47 PM
I'm currently looking to update an array via text fields in a form. Not exactly sure how to tackle this problem. What I want to happen is:

1) User fills out a form and clicks submit
2) The information the gets redisplayed

Currently this is what I have. Please help!

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
var answers = new Array(5);
answers[0] = document.question1.value;
answers[1] = document.form.question2.value;
answers[2] = document.form.question3.value;
answers[3] = document.form.question4.value;

function updateArray(value) {


function displayAnswers() {
document.writeln(answers[0]);
document.writeln(answers[1]);
document.writeln(answers[2]);
document.writeln(answers[3]);
}
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>
</HEAD>
<BODY>
<H1>Questionaire</H1>
<P>Please answer all of the fields below and click on submit when done.</P>
<FORM>
<P><B>1. Name</B></P>
<P><INPUT TYPE="text" NAME=question1 onChange="updateArray(this.value)"><BR></P>
<P><B>2. Address</B></P>
<P><INPUT TYPE="text" NAME=question2><BR></P>
<P><B>3. Occupation</B></P>
<P><INPUT TYPE="text" NAME=question3><BR></P>
<P><B>4. Phone</B></P>
<P><INPUT TYPE="text" NAME=question4><BR></P>
<P><INPUT TYPE=button VALUE="Submit" onClick="displayAnswers();"></P>
</FORM>

CodeNoob
12-10-2003, 10:42 PM
After some troubleshooting, I was able to figure it out. Just for anybodies interest, heres what I came up with:

<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS



function displayAnswers() {
var answers = new Array();
answers[0] = document.question.question1.value;
answers[1] = document.question.question2.value;
answers[2] = document.question.question3.value;
answers[3] = document.question.question4.value;

for (var count = 0; count < answers.length; ++count) {
document.writeln(answers[count] + "<br>");
}

}
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>
</HEAD>
<BODY>
<H1>Questionaire</H1>
<P>Please answer all of the fields below and click on submit when done.</P>
<FORM name="question">
<P><B>1. Name</B></P>
<P><INPUT TYPE="text" NAME=question1><BR></P>
<P><B>2. Address</B></P>
<P><INPUT TYPE="text" NAME=question2><BR></P>
<P><B>3. Occupation</B></P>
<P><INPUT TYPE="text" NAME=question3><BR></P>
<P><B>4. Phone</B></P>
<P><INPUT TYPE="text" NAME=question4><BR></P>
<P><INPUT TYPE=button VALUE="Submit" onClick="displayAnswers();"></P>
</FORM>

fredmv
12-11-2003, 04:31 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA[
function displayData()
{
var e = document.forms[0].elements;
var o = document.getElementById('out');
o.innerHTML = '';
for(var i=0; i<e.length; i++) if(e[i].type=='text') o.innerHTML += e[i].name.bold() + ': ' + e[i].value + '<br />';
}
//]]>
</script>
</head>
<body>
<form action="#">
<div>
<table summary="" style="width: 100%;">
<tbody>
<tr>
<td>Name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Occupation</td>
<td><input type="text" name="occupation" /></td>
</tr>
<tr>
<td>Phone</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td><input type="button" value="Display Form Data" onclick="displayData();" /></td>
</tr>
</tbody>
</table>
</div>
</form>
<div id="out"></div>
</body>
</html>