JavaScript / AJAX passes form fields as querystring - need to format textarea fro line breaks

tobzzz
tobzzz used Ask the Experts™
on
I'm passing form fields to an asp page that stores them in a database, the AJAX I have in place is sending the fields as a querystring. Where I am passing my "postal address" field, it allows for line break, e.g.
My Street Address
My Area of the City
I've used an alert box to debug and call see the querystring looks like:
"&address=My Street AddressMy Area of the City", so I need to some how format this so it will become@
"&address=My Street Address<BR>My Area of the City"

The code I am using is below...

Thanks experts!
var theForm = ""
var howManyElement = ""
var daString = ""
theForm = document.myform
howManyElement = theForm.elements.length;
xmlHttp=GetXmlHttpObject();
for (i=0; i<howManyElement; i++){
daString = daString + theForm.elements[i].name+ "="+theForm.elements[i].value+"&";
}
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url=whichLink;
url=url+"?"+daString;
url=url+"sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
 
// I don't think you need the rest of the code

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
Hi there,

The code below might do the trick.

Hope it helps ^_^
function return2br(str){ 
 return str.replace(/(\r\n|\r|\n)/g, "<BR>");
 
} 
var theForm = ""
var howManyElement = ""
var daString = ""
theForm = document.myform
howManyElement = theForm.elements.length;
xmlHttp=GetXmlHttpObject();
for (i=0; i<howManyElement; i++){ 
    if(theForm.elements[i].type == "textarea"){
        daString = daString + theForm.elements[i].name+ "="+return2br(theForm.elements[i].value)+"&";
    }else{
        daString = daString + theForm.elements[i].name+ "="+theForm.elements[i].value+"&";
    }
}
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url=whichLink;
url=url+"?"+daString;
url=url+"sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

Open in new window

Author

Commented:
@EZFrag: And THAT is why you have Master status :-) Thank you very much. Solved in one!

Author

Commented:
Excellent, thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial