www.webdeveloper.com
Results 1 to 4 of 4

Thread: need help collecting data

  1. #1
    Join Date
    Mar 2012
    Posts
    41

    need help collecting data

    this is part of the code
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <script>
    function AddInput(k) {
    			
    			var newSerial = document.createElement('input');
    			var newComment = document.createElement('input');
    			var newBreak = document.createElement('br')
    			
    			newSerial.type = "text";
    			newComment.type = "text";
    			
    			var parent = document.getElementById(k+"serial");
    			newSerial.id = "serial_"+k+"_"+eval("count"+k);
    			newComment.id = "comm_"+k+"_"+eval("count"+k);
    			
    			eval("count"+k+"+=1");
    			//eval("dynamic" + i + " = val[i]")
    			
    			parent.appendChild(newSerial);
    			parent.appendChild(newComment);
    			parent.appendChild(newBreak);	
    		}
    function GetSerials() {//data,radioAns) {
    		//THIS ALLOWS FUNCTIONALITY OF indexOF ON IE 
    		if(!Array.indexOf){
    			Array.prototype.indexOf = function(obj){
    				for(var i=0; i<this.length; i++){
    					if(this[i]==obj){
    						return i;
    					}
    				}
    				return -1;
    			}
    		}
    		
    		var counter = count = 0;
    		var k = 1;
    		var serials=[];
    		var comments=[];
    		var counters=[];
    			
    		var elements=document.getElementsByTagName("input");
    		
    		for (var i=0;i<elements.length;i++) 
    		{		
    			if (elements[i].id=="serial_"+k+"_"+counter)
    			{	
    				if(elements[i].className == "size_"+k)
    				{
    					//alert("here");
    					if (serials.indexOf(elements[i].value)==-1)
    					{
    						count++;						
    					} 
    					else 
    					{
    						alert("You can not use duplicate serial numbers - serial number "+elements[i].value+ " repeated");
    						elements[i].style.color = 'red';
    						return;
    					}				
    					counter++;		
    				}
    				k++;
    			}
    		}
    	}
    </script>
    </head>
    <body>
    	<div id="1serial">
    		Serial:	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    				&nbsp;&nbsp;&nbsp;&nbsp;
    				Comment:<br/>
    		<input type="text" id="serial_1_0" class="size_1"/><input type="text" id="comm_1_0"/><br/>
    	</div>
    	<input type="button" value="Add Pipe" onclick="AddInput(1)"><br/>
    			
    	<div id="2serial">
    		<strong>1502 Pipe 2' Joints</strong><br/>
    		Serial:	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    				&nbsp;&nbsp;&nbsp;&nbsp;
    				Comment:<br/>
    		<input type="text" id="serial_2_0" class="size_2"/><input type="text" id="comm_2_0"/><br/>
    	</div>
    	<input type="button" value="Add Pipe" onclick="AddInput(2)"><br/>
    	<!--There are "n" numbers of these-->
    </body>
    </html>
    I have a number of those inputs and at the moment what Im trying to do is to count how many serial numbers of a particular size exists, but I cant seem to wrap my head around it. Thanks for your help.

  2. #2
    Join Date
    Nov 2010
    Posts
    1,031
    let's start with some code that actually works, like I showed you a month ago:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <style type="text/css">
    #serial,#comment{
    float:left;
    }
    #butt{
    clear:both;
    }
    </style>
    
    </head>
    <body>
    <div id="serial">
    Serial:<br><input type="text" class="serials"/>
    </div>
    <div id="comment">
    Comment:<br><input type="text"/>
    </div>
    <div id="butt">
    <input type="button" onclick="checkDupes()" value="Add pipe"/>
    </div>
    <script>
    
    var serials=[];
    function checkDupes(){
    var tbs=document.getElementsByTagName("input");
    serials.length=0;
    for (var i = 0; i < tbs.length; i++) {
    if (tbs[i].type=="text"&&tbs[i].className=="serials"){
    tbs[i].style.color="black"
    if(!Array.indexOf){
    			Array.prototype.indexOf = function(obj){
    				for(var i=0; i<this.length; i++){
    					if(this[i]==obj){
    						return i;
    					}
    				}
    				return -1;
    			}
    		}
    if (serials.indexOf(tbs[i].value)==-1){
    serials.push(tbs[i].value);
    		} else {
    	alert("You can not use duplicate serial numbers - serial number "+tbs[i].value+ " repeated");
    	tbs[i].style.color="red"
    	return;
    			}
    		}
    	}
    var newSerial = document.createElement('input');
    var newComment = document.createElement('input');
    var serBreak = document.createElement('br');
    var comBreak = document.createElement('br')
    newSerial.type = "text";
    newSerial.className = "serials";
    newComment.type = "text";
    var serdiv = document.getElementById("serial");
    var comdiv = document.getElementById("comment");
    newSerial.id = "serial_"+serials.length;
    newComment.id = "comm_"+serials.length;
    serdiv.appendChild(serBreak);
    comdiv.appendChild(comBreak);
    serdiv.appendChild(newSerial);
    comdiv.appendChild(newComment);
    	
    }
    
    
    </script>
    </body>
    </html>
    now what was it that you wanted to do?

  3. #3
    Join Date
    Mar 2012
    Posts
    41
    well this info is going to be sent to a php file. The way this need to be sent is on 3 separate arrays, one with the counts of serials per size. This array will look like 3|2|3|...N.
    The second array will have the size followed by the serials number, and will look like size1|serial1|serial2|serial3|size2|serial1|serial2|sizeN|serialN. The last one will be a collection of all comments whether they exist or not, this will look like com1|com2|com3.
    So i think I am going to use getElementByClassName() to create the arrays of different elements.

  4. #4
    Join Date
    Nov 2010
    Posts
    1,031
    Quote Originally Posted by blak422 View Post
    So i think I am going to use getElementByClassName() to create the arrays of different elements.
    thus excluding anyone using <IE9 from interacting with your database?

    but more importantly, if you have code that prevents users from entering the same serial number, won't the "serials per size" always be 1|1|1?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles