473,388 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

getting form data

42
Hi,

I am trying to use this function to get the for data :


Expand|Select|Wrap|Line Numbers
  1. function getform(divid) { 
  2. var getstr=''; 
  3. var obj=document.getElementById(divid)? document.getElementById(divid) : document.forms[divid];
  4. for (i=0; i<obj.childNodes.length; i++) { 
  5. if (obj.childNodes[i].tagName == "INPUT") {
  6. if (obj.childNodes[i].type == "text") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;}
  7. if (obj.childNodes[i].type == "hidden") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;}
  8. if (obj.childNodes[i].type == "password") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;}
  9. if (obj.childNodes[i].type == "checkbox") { if (obj.childNodes[i].checked) {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;} else { getstr +="&" + obj.childNodes[i].name + "=off"; }}
  10. if (obj.childNodes[i].type == "radio") { if (obj.childNodes[i].checked) {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;}  else { getstr +="&" + obj.childNodes[i].name + "=off"; }}}   
  11. if (obj.childNodes[i].tagName == "SELECT") {var sel = obj.childNodes[i]; getstr +="&" + sel.name + "=" + sel.options[sel.selectedIndex].value;}} 
  12.  return getstr;}
  13.  
where the id or the name of the form is sent to this function and values are returned.

it works ok,

but when my fields are in a table it doesn't work, it finds the "table" tag as the childnode instead of input or select.

how can i make this work with tables?
Aug 10 '07 #1
7 2492
acoder
16,027 Expert Mod 8TB
Please post code using CODE tags. Thanks

Show your HTML markup.
Aug 10 '07 #2
xhunter
42
sure,

I have progressed it a little bit to get the childs, but it seems it is looping around the first <td><input> found.


let say this is the form :


[HTML]
<form id="form1" action="" method="post" onsubmit="return false;">
<table>
<tr>
<td>
<input type="text" name="var1" value="var1">
</td>
</tr>
<tr>
<td>
<input type="text" name="var2" value="var2">
</td>
</tr>
<tr>
<td>
<input type="submit" name="p6_Submit" value="submit" onclick="test('form1');"/>
</td>
<tr>
</table>
</form>
[/HTML]

using these functions, it loops round var1 and browser locks up because of this loop

here is the javascript code

Expand|Select|Wrap|Line Numbers
  1.  
  2. function test(form) {
  3. var obj=document.getElementById(form)? document.getElementById(form) : document.forms[form]; 
  4. alert(getform(obj));
  5. }
  6.  
  7. function getform(obj) { 
  8. var getstr='';
  9. for (i=0; i<obj.childNodes.length; i++) { 
  10. if (obj.childNodes[i].tagName == "INPUT") { 
  11. if (obj.childNodes[i].type == "text") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;} }   
  12. if (obj.childNodes[i].childNodes.length > 0) {getstr+=getform(obj.childNodes[i]);}
  13. return getstr;
  14. }
  15.  
  16.  
as you can see, I used this to loop the childes
[php]if (obj.childNodes[i].childNodes.length > 0) {getstr+=getform(obj.childNodes[i]);}[/php]

This should be a very common problem, I wonder why I couldn't find anything on it.
Aug 10 '07 #3
xhunter
42
Here is a full code,
if anybody could make it work, i would really appreciate it.

I am expecting an alert of "&p6_fullname=test&p6_email=test"

[HTML]
<html>
<head>
<script type="text/javascript">
function test(form) {
var obj=document.getElementById(form)? document.getElementById(form) : document.forms[form];
alert(getform(obj));
}

function getform(obj) {
var getstr='';
for (i=0; i<obj.childNodes.length; i++) {
if (obj.childNodes[i].childNodes.length > 0) {getstr+=getform(obj.childNodes[i]);}
else if (obj.childNodes[i].tagName == "INPUT") {
if (obj.childNodes[i].type == "text") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;} }
}
return getstr;
}
</script>
</head>
<body>
<form id="form1" onsubmit="return false;">
<table>
<tr>
<td>Full Name:</td>
<td><input type="text" name="p6_fullname" value="test" size="40" /></td>
</tr>
<tr>
<td>email:</td>
<td><input type="text" name="p6_email" value="test" size="40" /></td>
</tr>
<tr><td><input type="submit" name="p6_Submit" value="submit" onclick="test('form1');"/></td></tr>
</table>
</form>
</body>
</html>
[/HTML]
Aug 10 '07 #4
acoder
16,027 Expert Mod 8TB
Why not use getElementsByTagName?
Aug 13 '07 #5
xhunter
42
thanx, finally somebody,

how would I do that, how would get the childes of an specific form?
Aug 13 '07 #6
xhunter
42
thanx for pointing me to the right direction :),

made it work.

cheers..
Aug 13 '07 #7
acoder
16,027 Expert Mod 8TB
thanx for pointing me to the right direction :),

made it work.
That's what it's all about. Glad I could be of help. Post again if you have any more problems.
Aug 14 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steve | last post by:
I'm working in PHP, but am also using JavaScript, so perhaps there is a better way to do this using more PHP code...... I setup a search screen for my database, the user enters data and submits...
0
by: Willoughby Bridge | last post by:
Hi - Having trouble getting a multipart series of forms to move to the next form. The problem line is: <form method="post" enctype="multipart/form-data" action="Data_Form1.php"> If the...
2
by: Erik Johnson | last post by:
I am trying to work with a program that is trying make an HTTP POST of text data without any named form parameter. (I don't know - is that a normal thing to do?) I need to write a CGI program that...
18
by: lawrence | last post by:
If I'm pretty sure there is just one form on the page, can i do this? var myForm = document.forms; If I'm not sure about the form, is it safer to do this? if (document.forms) { var myForm =...
10
by: eggie5 | last post by:
Is it possible to get a file without using a form post? I want to get the data (bytes) of a file, text or binary, and just save it to a variable. Similar to the post body of a form that has a...
1
by: kallol | last post by:
Hi Everyone, I have a form designed with a table and the table is inside a <div> (Required. Can’t get rid of it). The div has style “height: 100% and scrollable: auto”. The issue: In IE, let...
2
by: shadowman | last post by:
So here's the situation: I need to write a PHP script which accepts form submissions using all methods (GET and POST) and all content types (application/x-www-form-url-encoded and...
3
by: JohnGos | last post by:
Since around 10th May (a couple of days after the recent IE autoupdate), a web application which has worked without problem for several years has developed intermittent problems with data posted from...
2
by: srusskinyon | last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
2
DonRayner
by: DonRayner | last post by:
This one has me stumped. I'm getting a "Type Mismatch" error on one of my forms when it's being opened. It's hapening before the forms "On Open" event, I stuck a msgbox in there to check and I'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.