Passing a form array to a JavaScript PHP-output function...?

I have a PHP file at the bottom of my markup that gets generated to output my JavaScript (many reasons for this…). In the markup, though, I have a form whose input values are stored within an array. I need to pass this array to a JavaScript function that gets executed upon FORM SUBMIT.

Inside the form tag, would I include an onsubmit attribute like so?

<form action="index.php" [B]onsubmit="return validate(this,new_chart);"[/B] method="post">

In my mind, this would call the validate() function that accepts 2 arguments (this, which represents the form being processed and the array being passed into the function).

The problem I’m running up against in all this is that Firebug keeps telling me that the array I’m trying to pass isn’t defined. I’m thinking that’s because I’m not passing the array properly or that it’s because the JavaScript function isn’t understanding that what I’m trying to pass is even there–of which my understanding of JavaScript in general shines. :wink:

The JavaScript that’s intended to handle the array is the following:

<?php

	echo '
		function validate(thisform,array){
			if(new_chart[email] !== NULL && new_chart[email] !== \\'\\'){
				alert(new_chart[\\'email\\']);
			}
		}
	';

?>

I think I need to tell this function that array is an actual array, but how? Something like “var new_array = for(x in array){iteration assignment}”?

How can I pass the array that’s created within the form over and into that JavaScript function? :confused:

Let me know if you’re lost–I can suck when explaining these things… :slight_smile:

The array of form elements is not passed around in the manner that you may think. It’s not an array either, instead it’s an “array-like structure”

You only need to pass the this keyword to the function, which is the reference back to the form.

From within the function itself, where you use thisform as the reference to the form, you can gain access to the form elements through the elements structure of the form.

thisform.elements.email.value

where thisform is the reference to the form, email is the name of the form element, and value is a property of the form element.

For example:


if (thisform.elements.email.value > "") {
    alert(thisform.elements.email.value);
}

Or


var el = thisform.elements.email;
if (el.value > "") {
    alert(el.value);
}

Oh! Hehe. I gotchya. That clears up a bunch of questions! :slight_smile:

Thanks, pmw57.