Unstoppable Domains

PHP+Javascript - Getting Multiple fields

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Impact
19
Hey
In a script im working on...i let users make Multiple fields [textboxes, checkboxes, Dropdowns]..when they submit the form..it goes to "Process.php" where it deos it stuff..but i cant figure out if i can use a forloop or something in process.php [or array] to get all the fields of a kind..for example
$textboxes = all the textboxes
$dropdowns = all the dropdowns..
and in javascript...to validate the form..
Thanks
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
well one way of doing it when your in process.php, if I understand you right is to name the inputs accordingly input_text_# ( i.e input_text_1, input_text_2 ) then in your process.php

PHP:
foreach( $_POST as $key => $value )
{
	if( strstr( $key , 'input_text_' ) )
	{
		//do text input processing here
	}
	
	if( strstr( $key , 'input_dropdown_' ) )
	{
		//do dropdown input processing here
	}
}

cheers,

Bax
 
0
•••
yup..and thats great:) i'll try that..what about in Javascript?
 
0
•••
bump
 
0
•••
Here's a function I wrote a while back that I've used successfully many times for processing forms in javascript. It's somewhat long, but such is the nature of the beast.

The function processes:
  • text/textarea/hidden/password text entry fields
  • checkboxes
  • radio buttons
  • single/multiple select boxes

The function ignores:
  • buttons (no point)
  • file uploads (javascript can't process those)

I modified the function a little here by adding a "TODO:" comment anywhere that you might want to process/use form data. Just search on that phrase and fill in whatever you want.

Code:
function validate_form(form)
{	
	// process each form element
	var len = form.elements.length;
	for (var i=0; i < len; i++) 
	{
		var elem = form.elements[i];
		
		if (elem.type == "checkbox" || elem.type == "radio") 
		{
			// TODO: item name and value in elem.name and elem.value
			
			if (elem.checked == true)
			{
				// TODO: item is checked/selected
			}
			else
			{
				// TODO: item is not checked/selected
			}
		}
		else if (elem.type == "select-multiple" || elem.type == "select-one")
		{
			var elems = new Array();
		
			// use childNodes and the associated node info instead of ".options"
			// because some browsers are happier this way (Opera, Camino, others?)
			var cnodes = elem.childNodes;
			var len2 = cnodes.length;
	
			// TODO: select/listbox name is in elem.name
			
			for (var j=0; j < len2; j++)
			{
				var item = cnodes[j];
	
				// TODO: list entry value is in item.innerHTML
				
				if (item.selected == true)
				{
					// TODO: menu/list entry is selected
				}
				else
				{
					// TODO: menu/list entry is not selected
				}
			}
		}
		else if (elem.type == "text" || elem.type == "textarea" || elem.type == "password" || elem.type == "hidden")
		{
			// TODO: item name and value in elem.name and elem.value
			if (elem.value != "")
			{
				// TODO: field is not empty
			}
			else
			{
				// TODO: field is empty
			}
		}
}
 
0
•••
hey
im confused to what i need to do here:
Code:
// TODO: item name and value in elem.name and elem.value
 
0
•••
Let's say you have a couple of text input fields on a form like this:

PHP:
<input type="text" name="username" />
<input type="password" name="password" />

In that block of code where the "TODO" item is, you would put a couple of "if" statements to check each value:
PHP:
if (elem.name == "username")
{
   // validate the user name, which is in elem.value
   // for example, don't allow blank usernames
   if (elem.value == "") 
            alert("Please enter a valid user name");
}
else if (elem.name == "password")
{
     // validate the password.  Must be at least 8 characters long
     if (elem.value.length < 8) 
            alert("Password must be at least 8 characters long");
}
 
0
•••
yes but its a dynamic form..generated by a for loop..so there are field names like

name1
name2
name3

how do i run it through a forloop in Javascript to get all te fields?
 
0
•••
The code I posted does that: it iterates through all the fields in the form. Here's a gutted version:

PHP:
// process each form element
var len = form.elements.length;
for (var i=0; i < len; i++) 
{
      var elem = form.elements[i];
      // do something with the form element
}

This loops through the form. Each time through, the current form field is stored in the variable "elem".

The code doesn't care what the names of the fields are, what type they are or how many there are.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back