SuperSal
Programmer
- Mar 13, 2007
- 33
Hi everyone, I know this seems a really simple thing to do but I just can't get it working correctly. Basically the code is creating a URL CGI form submission. The code gathers all the form elements names and values and appends it to the pages URL. The problem is that I need to replace certain characters such as whitespace so that the server doesnt throw an error. The code is as below...can anyone help me out as to replace the whitespace with a + character. Also is there any other characters that I will need to be aware of.
function get_formdata(form_data)
{
var theForm = document.forms[0];
var textData = ""
var URL = location.href;
for(i=0; i<theForm.elements.length; i++){
if (theForm.elements.value != "")
{
if(theForm.elements.type == "text" || theForm.elements.type == "textarea" || theForm.elements.type == "button")
{
textData += theForm.elements.name + "=" + theForm.elements.value +'&'
}
else if(theForm.elements.type == "checkbox")
{
textData += theForm.elements.name + "=" + theForm.elements.checked +'&'
}
else if(theForm.elements.type == "select-one")
{
textData += theForm.elements.name + "=" + theForm.elements.options[theForm.elements.selectedIndex].text +'&'
}
}
}
var URL1 = URL+='?'+textData;
alert(URL+='?'+textData);
var CGIURL = URL1.replace('\s','+'); //this bit not working
alert(CGIURL);
}
Thanks
Sally
function get_formdata(form_data)
{
var theForm = document.forms[0];
var textData = ""
var URL = location.href;
for(i=0; i<theForm.elements.length; i++){
if (theForm.elements.value != "")
{
if(theForm.elements.type == "text" || theForm.elements.type == "textarea" || theForm.elements.type == "button")
{
textData += theForm.elements.name + "=" + theForm.elements.value +'&'
}
else if(theForm.elements.type == "checkbox")
{
textData += theForm.elements.name + "=" + theForm.elements.checked +'&'
}
else if(theForm.elements.type == "select-one")
{
textData += theForm.elements.name + "=" + theForm.elements.options[theForm.elements.selectedIndex].text +'&'
}
}
}
var URL1 = URL+='?'+textData;
alert(URL+='?'+textData);
var CGIURL = URL1.replace('\s','+'); //this bit not working
alert(CGIURL);
}
Thanks
Sally