14 Replies - 2293 Views - Last Post: 19 May 2011 - 09:59 PM

#1 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Array prints out undefined elements

Posted 18 May 2011 - 10:20 PM

I have a small array which I will be printing out for email. The only thing that should be going into the array are the "Not OK" options that have been selected by the user, instead all the "Not OK" values are going into the array, and when the array is printed (documnet.write), the "Not OK" options that were not selected, show as 'undefined'.

Here is the js:

		var elem = document.getElementsByTagName('select');
		var comment = document.forms[0].comments.value;
		var str = "";
		
		var NotOkArray = new Array; 
		for (var i = 0; i < elem.length; i++)
		{
			if (elem[i].value == 'notOK')
			{
				NotOkArray[i] = elem[i].name;
			}
		}
		




Here is a portion of the html:

<dd>
					<select name="Left_Headlight" class="selectBox">
						<option value="N/A">N/A</option>
						<option value="OK">Okay</option>
						<option value="notOK">Needs Work</option>
						<option value="inspectorFix">Fixed by Inspector</option>
						<option value="mechanicFix">Fixed by Mechanic</option>
					</select>
				</dd>
				<dt><label>R. Headlight</label></dt>
				<dd>
					<select name="Right_Headlight" class="selectBox">
						<option value="N/A">N/A</option>
						<option value="OK">Okay</option>
						<option value="notOK">Needs Work</option>
						<option value="inspectorFix">Fixed by Inspector</option>
						<option value="mechanicFix">Fixed by Mechanic</option>
					</select>
				</dd>
				<dt><label>L.F. Turn</label></dt>
				<dd>
					<select name="Left_Front_Turn" class="selectBox">
						<option value="N/A">N/A</option>
						<option value="OK">Okay</option>
						<option value="notOK">Needs Work</option>
						<option value="inspectorFix">Fixed by Inspector</option>
						<option value="mechanicFix">Fixed by Mechanic</option>
					</select>
				</dd>





I tried changing line 8 to:

 if (elem[i].value == 'notOK' && elem[i].text == 'Needs Work')



But then nothing prints. Ideas?

This post has been edited by teahou: 18 May 2011 - 10:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Array prints out undefined elements

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Array prints out undefined elements

Posted 18 May 2011 - 11:15 PM

do you have a test page?
Was This Post Helpful? 0
  • +
  • -

#3 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Array prints out undefined elements

Posted 19 May 2011 - 10:26 AM

Yes:

http://auto.courtesy...inspection.html
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Array prints out undefined elements

Posted 19 May 2011 - 11:00 AM

your problem is line 34 (in the test page), namely document.write(). it is often forgotten that document.write(), when called after the page finished loading, will wipe out your whole document (that’s because document.write() will implicitly call document.open() if necessary)

the other problem is line 10 of the first code block, you assign each "notOK" value with the according index, i.e. the length of NotOkArray will always be the last inserted index +1. use Array.push() to get around that.

This post has been edited by Dormilich: 19 May 2011 - 11:05 AM

Was This Post Helpful? 1
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Array prints out undefined elements

Posted 19 May 2011 - 11:06 AM

You use the same index i for both in both arrays.
so, i increses for every loop.
but you assign only values when the element in that index has value of notOK.
what happens, is that your notOk array looks like:
0                 1           2            3           4  ....  (same size as number of select)
right brake   undefined   undefined    leftBrake   undefined

add another index, say, j, and change your code to:
var NotOkArray = new Array;
var j = 0;
for (var i = 0; i < elem.length; i++)
{
	if (elem[i].value == 'notOK')
	{
		NotOkArray[j] = elem[i].name;
                j++;
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Array prints out undefined elements

Posted 19 May 2011 - 12:04 PM

jaVanir, thank you that worked. i want to make sure I understand why. It looks to me that the 'i' index loops through ALL selects and creates an element within the array for ever select because every select contains a 'notok' option. Then, by using the same index of 'i' on the NotOKkArray, it was looping through the same length, but only entering valuse to the elements within the array that had actually been selected by the user, which resulted in all the undefined elements.

So, by changing the index of the NotOkArray to 'k', it is now only entering elements that are selected.

I think I understand, though it is still a little fuzzy. I understand why it was wrong, I don't fully understand why your solution worked.

If you have the time, I would appreciate if you could expand on your explanation a bit, or provide a link to a good source.

Thank you all for your responses.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Array prints out undefined elements

Posted 19 May 2011 - 12:09 PM

View Postteahou, on 19 May 2011 - 09:04 PM, said:

I think I understand, though it is still a little fuzzy. I understand why it was wrong, I don't fully understand why your solution worked.

because heís using a separate indexing variable for the other array. (although I deem Array.push() cleaner)
Was This Post Helpful? 0
  • +
  • -

#8 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Array prints out undefined elements

Posted 19 May 2011 - 12:19 PM

Dormilich, I tried your way and it also worked, like this?:

		for (var i = 0; i < elem.length; i++)
		{
			if (elem[i].value == 'notOK')
			{
				NotOkArray.push(elem[i].name);

			}
		}



I agree that it does seem cleaner.

Here is another question then. I now need to take the data in this array and email it to a specific email address. I know I can put the email address in the 'action' property of the form tag, but that would send the entire form information, when I only want the info in the array to be sent.

Is there a way to send just the array data with javascript, or should I be looking at PHP?
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Array prints out undefined elements

Posted 19 May 2011 - 12:22 PM

PHP or any other server-side language you like. thatís definitely the better choice.
Was This Post Helpful? 0
  • +
  • -

#10 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Array prints out undefined elements

Posted 19 May 2011 - 12:41 PM

Is there a way to send the data in my NotOkArray to a PHP file for emailing, or do I need to send the entire form and recreate my NotOkArray in the php file?
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Array prints out undefined elements

Posted 19 May 2011 - 12:42 PM

by using AJAX you can send what you like to the server, whereas a form submit sends all available form data (though that does not necessarily mean that you have to use all of them).

This post has been edited by Dormilich: 19 May 2011 - 12:43 PM

Was This Post Helpful? 0
  • +
  • -

#12 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Array prints out undefined elements

Posted 19 May 2011 - 01:54 PM

Does PHP have anything similar to getElementById and so forth? I have never used PHP (that is next semester) and I am trying to figure out how to handle this form.

Ideally I would figure out how to send the data i have in my javascript NotOkArray to php, and send it an email from there, but I have yet to figure out how to do that.

The other option is to just submit the form with a submit button, so everything goes over, then create a function PHP to pull out all the 'notOK' values from the many select boxes.

I already have the notokarray created and working js, so that would be my first option (to send the array data to php, then into an email from php.

Otherwise, I have put this php code together, but there are close to 100 dropdowns on the page, which would mean a ton of coding doing it this way:

<?php

	
	$NotOk = array();
	
	$leftHeadlight=$_POST['Left_Headlight'];
	if ($leftHeadlight)
	{
	 	foreach ($leftHeadlight as $option)
	 	{
	 		if ($option == 'notOK')
	 		{
	 			NotOk_push($option);
	 		}
	 	}
	 }
	 
 	$rightHeadlight=$_POST['Right_Headlight'];
	if ($rightHeadlight)
	{
	 	foreach ($rightHeadlight as $option)
	 	{
	 		if ($option == 'notOK')
	 		{
	 			NotOk_push($option);
	 		}
	 	}
	 }

?>



Please let me know your suggestion on the easiest way to do this. please remember i have not yet sudied PHP, but I have a book, which I have been reading.
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Array prints out undefined elements

Posted 19 May 2011 - 02:03 PM

there are some misconceptions. PHP runs on the server and doesn’t care at all about the HTML page itself (the page is just text to PHP, unlike the situation for JS).

despite that, PHP does know getElementById() (though it’s useless for your purpose).

second, a form submit will only submit a <select>’s selected value (not all options), so foreach ($leftHeadlight as $option) will quit with an error (Array or Traversable expected, String given). you would only need a filter on the input array, like
function notOK($val)
{
    return ($val === "notOK");
}
$notOK = array_filter($_POST, "notOK");


PS. you can use the exact same concept in JS …
var sel = document.getElementsByTagName("select");
var notOK = [].filter.call(sel, function (el) {
    return (el.value === "notOK");
});

This post has been edited by Dormilich: 19 May 2011 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#14 teahou  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 223
  • Joined: 01-April 11

Re: Array prints out undefined elements

Posted 19 May 2011 - 03:25 PM

I am working with this, here is what I have:


<?php

	
function notOK($val)
{
	return ($val === "notOK");
}

$notOK = array_filter($_POST, "notOK");

for ($i = 0; $i <= $notOK; $i++)
{
	echo $notOK[$i];
}
?>




I am trying to print out the array to ensure it is working, but nothing is happening. i also tried:

print_r($notOK);



but nothing happened.
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: Array prints out undefined elements

Posted 19 May 2011 - 09:59 PM

what happens when you print print_r($_POST); ?

besides that, your for() syntax is off.

This post has been edited by Dormilich: 19 May 2011 - 10:00 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1