Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through form elements + creating a URL for cgi form submission 1

Status
Not open for further replies.

SuperSal

Programmer
Mar 13, 2007
33
Hi everyone, I have a form which i need to loop through all of the elements in the form and display them as one long string (for cgi form submission). I have written the code below which loops through the different types and displays them into an alert box. How do I now get all the data in one long string rather than them looping through and picking one out at a time?

function get_formdata(form_data)
{
var theForm = document.forms[0]

for(i=0; i<theForm.elements.length; i++){

var textData = ""

if(theForm.elements.type == "text" || theForm.elements.type == "textarea" || theForm.elements.type == "button"){
textData += theForm.elements.name + "=" + theForm.elements.value + "\n"
}
else if(theForm.elements.type == "checkbox"){
textData += theForm.elements.name + "=" + theForm.elements.checked + "\n"
}
else if(theForm.elements.type == "select-one"){
textData += theForm.elements.name + "=" + theForm.elements.options[theForm.elements.selectedIndex].text + "\n"
}
alert(textData)
}
}

Thanks
Sally
 
Thanks Adam, I have been told to do it this way (by my boss) so I darn't really argue. He has said that we are going to need to manage other content to be sent to the server such as bitmasks. I wasn't well up on form submission so I didn't argue the case at the time, although I'm sure there will be a way to do what he wants from simply submitting the form like you said.

Anyway from moving the textdata variable out of the loop makes it so that it goes through the form elements adding each one one after another, so i get an alertbox with the first one, then another alertbox with the first and second one etc etc.

Is there a way to get one messagebox with all the form data together?

Thanks
Sally
 
Thanks but I have done this now, just needed to move the alert out of the inner loop. Here is the code:

function get_formdata(form_data)
{
var theForm = document.forms[0]
var textData = ""
for(i=0; i<theForm.elements.length; i++){

if(theForm.elements.type == "text" || theForm.elements.type == "textarea" || theForm.elements.type == "button"){
textData += theForm.elements.name + "=" + theForm.elements.value + "\n"
}
else if(theForm.elements.type == "checkbox"){
textData += theForm.elements.name + "=" + theForm.elements.checked + "\n"
}
else if(theForm.elements.type == "select-one"){
textData += theForm.elements.name + "=" + theForm.elements.options[theForm.elements.selectedIndex].text + "\n"
}
}
alert(textData)
}

Thanks Adam for your help.

Sally
 
Thanks Adam, I have been told to do it this way (by my boss) so I darn't really argue.

You need a new boss that actually knows what he's doing.

I think Adam should be your new boss.

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top