[thelist] Fwd: Javascript array loop issues?

Tris beertastic at gmail.com
Fri Oct 30 05:35:25 CDT 2009


Cheers all!

So basically, what I'm trying to do is develop an ajax driven form checker
all is working, but I am generating the error array in PHP and echoing
it to the javascript.

I've pasted my code below... Feel free to throw it on a PHP enabled
server to see what's what..
If the $_SESSION['error'] element has a vale, I wanna do X, and if it
doesn't, I wanna do Y..

anyhoo, here's the code I'm using...
I'm sure I'm just being dense... Javascript aint my bag baby...

================================================

<?
session_start();

$_SESSION['error'] = array( `lookingWhat` => 'Who are you looking for?'
							`genderWhat` => 'What gender are you?',
							`passwordDifferent` => '',
							`emailRegistered` => '',
							`emailInvalid` => 'Invalid Email Address',
							`FieldsEmpty` => 'Pleace complete all fields'
);

if ($_SESSION['error']) {
	foreach($_SESSION['error'] as $getErrorName => $getErrorText) {
		if ($getErrorText)
			$error .= $getErrorText . '<br />';
		$errorArray .= '' . $getErrorName . ':"' . $getErrorText . '", ';
	}
	$errorArray = substr($errorArray, 0, -2);
}
?>
<script type="text/javascript">

	alert('start');

	var getErrors = [<?=$errorArray ?>];

	for ( var i=0, j=getErrors.length(); i<j; i++ ) {
		var formElement = $('#'+getErrors[i].errorKey);
		if (formElement.val() == '') {
			alert('Error field ' + formElement.val() + ' is blank');
		} else {
			alert('Error field ' + formElement.val() + ' has content');
		}
	}

	alert('end');
	
</script>

<?=$error ?>

<? unset($_SESSION['error']); ?>

=============================================================



2009/10/29 Jay Turley <jayturley at gmail.com>:
> Ever have one of those days where no matter what you write, it's wrong?
>
> Length() should be just length.
>
> <tip type="reputation">
> Don't answer problems unless you take the time to make sure you
> understand both the problem AND your answer.
> </tip>
>
> Le sigh
>
>
>
> Begin forwarded message:
>
>> From: Jay Turley <jayturley at gmail.com>
>> Date: October 29, 2009 12:32:15 PM PDT
>> To: "thelist at lists.evolt.org" <thelist at lists.evolt.org>
>> Subject: Re: [thelist] Javascript array loop issues?
>>
>
>> Yes, as I shamefacedly pointed out in my second email (which I then
>> screwed up as well), replying from your phone is a sure way to lower
>> your street cred.
>>
>> If I were doing this, using the idea that Tris was demonstrating in
>> her initial email, I would try something along the following lines:
>>
>> <input id="name" type="text" />
>> <input id="email" type="text" />
>>
>> var getErrors = [
>>  {
>>   errorKey: 'name',
>>   errorMsg: 'Please enter your name'
>>  },
>>  {
>>   errorKey: 'email',
>>   errorMsg: 'Please enter your email'
>>  }
>> ];
>>
>> for ( var i=0, j=getErrors.length(); i<j; i++ ) {
>>  var $formElement = $('#'+getErrors[i].errorKey);
>>  if ( $formElement.val() == '' ) {
>>   alert(getErrors[i].errorMsg);
>>  }
>> }
>>
>> or something...
>>
>> at least that looks reasonably correct in my text editor... ON MY
>> COMPUTER :(
>>
>> On Thu, Oct 29, 2009 at 11:07 AM, Christian Heilmann <codepo8 at gmail.com
>> > wrote:
>> Jay Turley wrote:
>> > I think your problem may be that you are creating an object
>> literal by
>> > using the braces {} instead of the brackets [] to create your array
>> > elements. Try it like this:
>> >
>> > var getErrors = [ errorName: errorMsg, ...];
>> >
>> > Jay
>> >
>> >
>> >
>> No, a "associative array" is a misnomer as in JS it is an object. You
>> cannot loop over the properties of an object using length, what you
>> need
>> to do is using a for...in loop.
>>
>> http://stackoverflow.com/questions/921789/how-to-loop-through-javascript-object-literal-with-objects-as-members
>>
>>
>> --
>>
>> * * Please support the community that supports you.  * *
>> http://evolt.org/help_support_evolt/
>>
>> For unsubscribe and other options, including the Tip Harvester
>> and archives of thelist go to: http://lists.evolt.org
>> Workers of the Web, evolt !
>>
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>



-- 


==========================

"The only people who don't make mistakes are those who don't do anything"

Give a man a fish and he'll feed himself for a day.
Give a man a religion and he'll starve to death praying for a fish.
Anon

`We are what we pretend to be, so we must be careful what we pretend to be.`
Kurt Vonnegut

`When a person can no longer laugh at himself, it is time for others
to laugh at him.`
Thomas Szasz



More information about the thelist mailing list