NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP+Javascript - Getting Multiple fields

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 03-15-2007, 08:42 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes

PHP+Javascript - Getting Multiple fields


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
unknowngiver is offline  
Old 03-15-2007, 02:33 PM   #2 (permalink)
NamePros Regular
 
baxter's Avatar
Join Date: Apr 2006
Posts: 363
baxter is just really nicebaxter is just really nicebaxter is just really nicebaxter is just really nice
 


Ethan Allen Fund Save The Children
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 Code:
foreach( $_POST as $key => $value )
{
    if( 
strstr$key 'input_text_' ) )
????: NamePros.com http://www.namepros.com/programming/305294-php-javascript-getting-multiple-fields.html
    {
        
//do text input processing here
    
}
    
    if( 
strstr$key 'input_dropdown_' ) )
    {
        
//do dropdown input processing here
    
}

cheers,

Bax
__________________
Canadian Domain Registrar Ready.ca
baxter is offline  
Old 03-15-2007, 04:20 PM THREAD STARTER               #3 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
yup..and thats great i'll try that..what about in Javascript?
unknowngiver is offline  
Old 03-16-2007, 10:31 PM THREAD STARTER               #4 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
bump
unknowngiver is offline  
Old 03-17-2007, 07:27 PM   #5 (permalink)
cef
NamePros Regular
Join Date: May 2004
Location: NYC
Posts: 236
cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough
 


Animal Rescue
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
			}
		}
}
cef is offline  
Old 05-10-2007, 02:54 PM THREAD STARTER               #6 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
hey
im confused to what i need to do here:
Code:
// TODO: item name and value in elem.name and elem.value
unknowngiver is offline  
Old 05-10-2007, 04:43 PM   #7 (permalink)
cef
NamePros Regular
Join Date: May 2004
Location: NYC
Posts: 236
cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough
 


Animal Rescue
Let's say you have a couple of text input fields on a form like this:

PHP Code:
<input type="text" name="username" />
????: NamePros.com http://www.namepros.com/showthread.php?t=305294
????: NamePros.com http://www.namepros.com/showthread.php?t=305294
<
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 Code:
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");

cef is offline  
Old 05-10-2007, 05:50 PM THREAD STARTER               #8 (permalink)
Senior Member
Join Date: May 2005
Location: Ontario Canada
Posts: 3,088
unknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to beholdunknowngiver is a splendid one to behold
 


Diabetes
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?
unknowngiver is offline  
Old 05-10-2007, 06:12 PM   #9 (permalink)
cef
NamePros Regular
Join Date: May 2004
Location: NYC
Posts: 236
cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough
 


Animal Rescue
The code I posted does that: it iterates through all the fields in the form. Here's a gutted version:

PHP Code:
// process each form element
var len form.elements.length;
for (var 
i=0leni++) 
{
      var 
elem form.elements[i];
      
// do something with the form element
????: NamePros.com http://www.namepros.com/showthread.php?t=305294

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.
cef is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 02:14 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger