| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| Senior Member Join Date: May 2005 Location: Ontario Canada
Posts: 3,088
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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
__________________ |
| |
| | #2 (permalink) |
| NamePros Regular Join Date: Apr 2006
Posts: 363
![]() ![]() ![]() ![]() | 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: Bax
__________________ Canadian Domain Registrar Ready.ca |
| |
| | #5 (permalink) |
| NamePros Regular Join Date: May 2004 Location: NYC
Posts: 236
![]() ![]() ![]() | 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:
The function ignores:
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
}
}
} |
| |
| | #7 (permalink) |
| NamePros Regular Join Date: May 2004 Location: NYC
Posts: 236
![]() ![]() ![]() | Let's say you have a couple of text input fields on a form like this: PHP Code: PHP Code: |
| |
| | THREAD STARTER #8 (permalink) |
| Senior Member Join Date: May 2005 Location: Ontario Canada
Posts: 3,088
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | 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?
__________________ |
| |
| | #9 (permalink) |
| NamePros Regular Join Date: May 2004 Location: NYC
Posts: 236
![]() ![]() ![]() | The code I posted does that: it iterates through all the fields in the form. Here's a gutted version: PHP Code: The code doesn't care what the names of the fields are, what type they are or how many there are. |
| |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |