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
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