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

javascript submit an array

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
Hey all I have a question for ya. Using the example that I have been using I want to create a bunch of text boxes, loop through them and put them into an array, then submit the array. Here is my code.
Code:
<html>
<head>
	<title>Want to see if I can do this</title>
	<script type="text/javascript">

	var fieldCount = 0;	
	function addBreak()
    {
        var aBreak = document.createElement('br');
        document.getElementById('fieldHere').appendChild(aBreak);
        
    }
    function AddField()
        {
            fieldCount++;
            var addField     = document.createElement('input');
            addField.type     = 'text';
            addField.name     = 'field' + fieldCount;
            addField.id        = 'field' + fieldCount;
            document.getElementById('fieldHere').appendChild(addField);
            addBreak();
        }		
	function getInfo()
	{
		// Get the top level div and then searc for all the inputs
		var inputArray = document.getElementById("topOne").getElementsByTagName("input");
		var inputArrayLength = inputArray.length;
		   for (a = 0; a < inputArrayLength; a++) {
		      //filter out the textboxes here
		      if (inputArray[a].type == "text") {
		         alert(inputArray[a].value);
		      }
		   }
	}
	
	function clearAll()
	{
		 document.getElementById('fieldHere').innerHTML = "";
	}
	
	function submitAll()
	{
		var collectArray = new Array();
		// Get the top level div and then searc for all the inputs
		var inputArray = document.getElementById("topOne").getElementsByTagName("input");
		var inputArrayLength = inputArray.length;
		   for (a = 0; a < inputArrayLength; a++) {
		      //filter out the textboxes here
		      if (inputArray[a].type == "text") {
		        collectArray.push(inputArray[a].value);
		      }
		   }
		   // now I need to submit the array
		   
	}
	
	
	</script>
</head>
<body>
	<?php
		$getStuff = $_POST['stuff'] ;
		echo $getStuff;
	?>
	<!--<form name="myform" action="7.php"  method="POST"> -->
		<div id="topOne">
			<input type="button" value="Old Way" onclick="AddField()">
			<div id="fieldHere">
			</div>
			<input type="button" value="Get Info" onclick="getInfo()"/>
			<input type="button" value="Clear"	onclick="clearAll()"/>
			<input type="button" value="Submit" onclick="submitAll()"/>
		</div>
	</form>
</body>
</html>

I have create a button
Code:
<input type="button" value="Submit" onclick="submitAll()"/>
that will run this function
Code:
	function submitAll()
	{
		var collectArray = new Array();
		// Get the top level div and then searc for all the inputs
		var inputArray = document.getElementById("topOne").getElementsByTagName("input");
		var inputArrayLength = inputArray.length;
		   for (a = 0; a < inputArrayLength; a++) {
		      //filter out the textboxes here
		      if (inputArray[a].type == "text") {
		        collectArray.push(inputArray[a].value);
		      }
		   }
		   // now I need to submit the array
so I now hace this array inputArray and I want to submit that data in a post. Will this work?
Code:
document.submit(inputArray);

How can I submit this array
thanks
-T

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
You can't submit an array, the way to do it is to turn the array into a string.

Use the .toString() method to accomplish that.

After submitting, use the .split method on the string to change it back into an array.

[monkey][snake] <.
 
OK, thanks. So I converted the array to string and now I need to submit it. I dont want to submit the html form because I am creating the textboxes on the dynamically.

Code:
function submitAll()
	{
		var collectArray = new Array();
		// Get the top level div and then searc for all the inputs
		var inputArray = document.getElementById("topOne").getElementsByTagName("input");
		var inputArrayLength = inputArray.length;
		   for (a = 0; a < inputArrayLength; a++) {
		      //filter out the textboxes here
		      if (inputArray[a].type == "text") {
		        collectArray.push(inputArray[a].value);
		      }
		   }
		   var collectString = collectArray.toString();
		   document.submit(collectString);
	}

Thanks for the greate help
-T

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
Code:
document.submit(collectString);

When something is submitted (call it thing), it specifically means a HTML form is submitted that contains (thing).

You have to submit the form in order to retrieve the data server side.

The only other option I can think of is to use the QueryString.

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top