5 Replies - 494 Views - Last Post: 06 December 2012 - 03:57 AM

#1 mklein52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

issue with search button

Posted 05 December 2012 - 04:10 PM

i have created a search button for the arrays i need to search by last name but when i click my button the first name last name and salary pop up as undefined any help would be great



<!doctype html>

<html>
<head>
<title>test</title>
</head>

<script>

	var i = 0;
	
	var namearray = new Array();
	var lastarray = new Array();
	var salaryarray = new Array();
	var q;
	
	var g;
	function resetbutton () {
	var x = document.getElementById('fname');
	x.value = "";
	var y = document.getElementById('lname');
	y.value = "";
	var z = document.getElementById('salary');
	z.value = "";
	}//end resetbutton
	


function BlankCheck () {
	
	var errormessage = "";

	
	if (document.getElementById('fname').value == "") {
		errormessage +="enter your first name \n";
		document.getElementById('fname').style.borderColor = "red";
		}
	
	if (document.getElementById('lname').value == "") {
		errormessage +="enter your last name \n";
		document.getElementById('lname').style.borderColor = "red";
		}
		
	if (document.getElementById('salary').value == "") {
		errormessage +="enter salary \n";
		document.getElementById('salary').style.borderColor = "red";
		}	
		
	if (errormessage != "") {
		alert(errormessage);
		return false;
		}
		
	else {
		alert('thank you');
		namearray[i] = (document.getElementById('fname').value);
		lastarray[i] = (document.getElementById('lname').value);
		salaryarray[i] = (document.getElementById('salary').value);
		i++;
		var g=0;
        document.getElementById('output').innerHTML='';	  
			
			for(g=0; g < namearray.length; g++) {
				
				document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + namearray[g]+" " + lastarray[g]+ " " + salaryarray[g] +"<br>";
			}			
}
		namearray[i] = (document.getElementById('fname').value);
		lastarray[i] = (document.getElementById('lname').value);
		salaryarray[i] = (document.getElementById('salary').value);
		i++;
			
		for(g=0; g < namearray.length; g++) {
			document.getElementById('output').innerHTML='';	 
			document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + namearray[g]+" " + lastarray[g]+ " " + salaryarray[g] +"<br>";
			}//end for			
}//blank check


function search() {	
	for(q=0;q<lastarray.length;q++){
			if(document.getElementById('find').value==lastarray[q]){
				document.getElementById("output").innerHTML=document.getElementById("output").innerHTML+namearray[g]+" "+lastarray[g]+" "+salaryarray[g]+"<br/>";
		}//end if
	}//end for
}//end search()	
	

</script>



<body>
	<h1>test</h1>
	
<fieldset>
<label>First Name:
<input type="text" id="fname" name="fname"></label>
<br>
<label>Last Name:
<input type="text" id="lname" name="lname"></label>
<br>
<label>Salary:
<input type ="text" id="salary" name="salary"></label>
<br>
<input type="submit" value="send data" onclick="BlankCheck();">
<input type="button" value="clear" onclick="resetbutton();">
</fieldset>

<input type ="text" id="find" name="find"/>
<input type="button" value="search by last name"
 onclick="search();
 "/>

<div id="output"></div>	
	
</body>
</html>
	



Is This A Good Question/Topic? 0
  • +

Replies To: issue with search button

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,233
  • Joined: 08-June 10

Re: issue with search button

Posted 05 December 2012 - 04:28 PM

the resetbutton() function is superfluous. a standard reset-type button can do that better.

second, why do teachers come up with such flawed assignments? from a design point-of-view, you are highly susceptibly to data integrity failures. that assignment is a prime example of how not to do it. (see also this thread)

third, you can also use the push() method of the arrays to shove the values in.

fourth, q shouldn’t be a global, it’s a simple loop counter. you may get all kinds of side effects there.

line #75, using the incremental assignment += would shorten the code considerably.

your search() function doesn’t need a loop. arrays have a method for searching their contents (named indexOf()).

in your search() function you use g to display the values from the arrays, but the index of the matching value is stored in q.
Was This Post Helpful? 0
  • +
  • -

#3 mklein52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

Re: issue with search button

Posted 05 December 2012 - 05:31 PM

i have it working the way i want to now thanks but when it searches it displays it twice do you know how to fix that?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,233
  • Joined: 08-June 10

Re: issue with search button

Posted 05 December 2012 - 05:34 PM

impossible to say without the actualised code.
Was This Post Helpful? 0
  • +
  • -

#5 mklein52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 31-October 12

Re: issue with search button

Posted 05 December 2012 - 05:42 PM

i know its a mess and doesnt look very good i just want to get it all working then ill mess with making it look pretty


<!doctype html>

<html>
<head>
<title>test</title>
</head>

<script>

	var i = 0;
	
	var namearray = new Array();
	var lastarray = new Array();
	var salaryarray = new Array();
	var q;
	var w;
	var a;
	var g;
	function resetbutton () {
	var x = document.getElementById('fname');
	x.value = "";
	var y = document.getElementById('lname');
	y.value = "";
	var z = document.getElementById('salary');
	z.value = "";
	}//end resetbutton
	


function BlankCheck () {
	
	var errormessage = "";

	
	if (document.getElementById('fname').value == "") {
		errormessage +="enter your first name \n";
		document.getElementById('fname').style.borderColor = "red";
		}
	
	if (document.getElementById('lname').value == "") {
		errormessage +="enter your last name \n";
		document.getElementById('lname').style.borderColor = "red";
		}
		
	if (document.getElementById('salary').value == "") {
		errormessage +="enter salary \n";
		document.getElementById('salary').style.borderColor = "red";
		}	
		
	if (errormessage != "") {
		alert(errormessage);
		return false;
		}
		
	else {
		alert('thank you');
		namearray[i] = (document.getElementById('fname').value);
		lastarray[i] = (document.getElementById('lname').value);
		salaryarray[i] = (document.getElementById('salary').value);
		i++;
		var g=0;
        document.getElementById('output').innerHTML='';	  
			
			for(g=0; g < namearray.length; g++) {
				
				document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + namearray[g]+" " + lastarray[g]+ " " + salaryarray[g] +"<br>";
			}			
}
		namearray[i] = (document.getElementById('fname').value);
		lastarray[i] = (document.getElementById('lname').value);
		salaryarray[i] = (document.getElementById('salary').value);
		i++;
			
		for(g=0; g < namearray.length; g++) {
			document.getElementById('output').innerHTML='';	 
			document.getElementById('output').innerHTML = document.getElementById('output').innerHTML + namearray[g]+" " + lastarray[g]+ " " + salaryarray[g] +"<br>";
			}//end for			
}//blank check


function search() {	
	for(q=0;q<lastarray.length;q++){
			if(document.getElementById('find').value==lastarray[q]){
				document.getElementById("output").innerHTML=document.getElementById("output").innerHTML+namearray[q]+" "+lastarray[q]+" "+salaryarray[q]+"<br/>";
		}//end if
	}//end for
}//end search()	

function searchforsalary() {	
	for(w=0;w<salaryarray.length;w++){
			if(document.getElementById('find2').value==salaryarray[w]){
				document.getElementById("output").innerHTML=document.getElementById("output").innerHTML+namearray[w]+" "+lastarray[w]+" "+salaryarray[w]+"<br/>";
		}//end if
	}//end for
}//end search()	

	function averagesalary(){
				var sum=0;
				for(a=0;a<salaryarray.length;a++){
				sum =sum + salaryarray[a];
				}//end for loop
				average=sum/salaryarray.length;
				document.getElementById("output").innerHTML=average;
				//alert(average);
			}//end amount

</script>



<body>
	<h1>test</h1>
	
<fieldset>
<label>First Name:
<input type="text" id="fname" name="fname"></label>
<br>
<label>Last Name:
<input type="text" id="lname" name="lname"></label>
<br>
<label>Salary:
<input type ="text" id="salary" name="salary"></label>
<br>
<input type="submit" value="send data" onclick="BlankCheck();">
<input type="button" value="clear" onclick="resetbutton();">
</fieldset>

<input type ="text" id="find" name="find"/>
<input type="button" value="search by last name"
 onclick="search();
 "/>
<br>
 <input type ="text" id="find2" name="find2"/>
<input type="button" value="search by salary"
 onclick="searchforsalary();
 "/>
 
 <input type ="button" value="find average salary"
	onclick="averagesalary();
	"/>

 
<div id="output"></div>	
	
</body>
</html>
	

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,233
  • Joined: 08-June 10

Re: issue with search button

Posted 06 December 2012 - 03:57 AM

the double output comes from th double input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1