jQuery.ajax post losing spaces and line feeds

nap0leon
nap0leon used Ask the Experts™
on
Using jQuery's .ajax to post a form.
Spaces in the fields that are being posted over are disappearing.
Please advise.

sample:
web page has a simple form with input fields for name, email, subject, and message.  message is a text area, the others are input type=text.

Here is the jQuery that posts the information to my "send_email.asp" page.
The alert half way through the page shows that spaces and line feeds in the text area are intact at that point.
$(function() {  
	$('.error').hide();  
	$(".button").click(function() {  
		// validate and process form here  
		$('.error').hide();  
		var name = $("#name").val();  
		if (name == "") {  
			$("#name_error").show();  
			$("#name").focus();  
			return false;  
		}  

		var email = $("#email").val();  
		if (email == "") {  
			$("#email_error").show();  
			$("#email").focus();  
			return false;  
		}  

		var subject = $("#subject").val();  
		if (subject == "") {  
			$("#subject_error").show();  
			$("#subject").focus();  
			return false;  
		}  

		var message = $("#message").val();  
		if (message == "") {  
			$("#message_error").show();  
			$("#message").focus();  
			return false;  
		}  

		var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&message=' + message;  
alert(message);
		$.ajax({  
			type: "POST",  
			url: "send_email.asp",  
			data: dataString,  
			success: function() {  
				$('#contact_form').html("<div id='message'></div>");  
				$('#message').html("<h2>Contact Form Submitted!</h2>")  
				//.append("<p><img id='checkmark' height='25' width='25' src='images/checkmark.jpg' />&nbsp;I will be in touch soon.</p>")  
				.append("<p>I will be in touch soon.</p>")  
				.hide()  
				.fadeIn(1500);  
			},
			error: function() {
				$('#contact_form').html("<div id='message'></div>");  
				$('#message').html("<h2>Oops!  An error occured.</h2>")  
				//.append("<p><img id='checkmark' height='25' width='25' src='images/error.gif' />&nbsp;Please send an email the old fashioned way to jason@jasondahlin.com.</p>")  
				.append("<p>Please send an email the old fashioned way to jason@jasondahlin.com.</p>")  
				.hide()  
				.fadeIn(1500);
			}
		});  

		return false; 
	});
});

Open in new window


e.g., if my message is
"Hello there.

This is a test"

The email I receive comes across with:
"Hellothere.Thisisatest"

If I remove the ajax and do a regular form-post to my send_email.asp page, the message (and subject) come across just as they should:
"Hello there.

This is a test"

So... it appears that there is something going on in the ajax posting of the information that is screwing up the formatting.  Any ideas?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You need to replace all spaces in "message" with %20 before you send it.

Change line:

var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&message=' + message;  

Open in new window


to this:

var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&message=' + message.replace(" ","%20");  

Open in new window

Did that fix it? :)
Top Expert 2011

Author

Commented:
Ah yes, URL encoding.
Interesting that it affects an ajax post, but not a regular form-post.

"escape()" works great, except one small issue - line feeds break it!

For example, if I escape it like this:
var dataString = 'name='+ escape(name) + '&email=' + escape(email) + '&subject=' + escape(subject) + '&message=' + escape(message);

Open in new window

the escaped message comes across as:
This%20is%20a%20test.%0A%0AIt%20is%203%20lines%20long.

Open in new window


But it throws an error.
Checking it on my QSParam version of the page with that in the URL string, it also croaks.  Removing the "%0A" from the URL and it works.

The error number is error '80040211', but all my research says that error means I am using the wrong authentication, which is not the case, because as soon as I remove the %0A from the URL, it works.

Inside send_email.asp, I tried replacing the %0A with a vbCRLF, but that did not seem to do anything.

A bit more research suggested using encodeURI instead of escape() with the same results.

If my "message" contains a carriage return in it, the ajax post fails.  If my message does not contain any carriage returns, it works.

I'll keep plugging away... maybe I need to replace the line feeds ("\n" maybe) from the text area with something that I can pick up in the ASP on the other side and turn into vbCRLFs.  But I'm sure there is a canned solution to this,
Top Expert 2011

Author

Commented:
I have it working, but would prefer a more robust solution:

my dataString in "conact.asp" replaces \n with <br>
var dataString = 'name='+ escape(name) + '&email=' + escape(email) + '&subject=' + escape(subject) + '&message=' + escape(message.replace("\n","<br>"));

Open in new window


then in my send_email.asp, I replace <br> with vbCRLFs
Message = Replace(Request.Form("message"),"<br>",vbCRLF)

Open in new window


Since I'm not using HTML email, this is OK... but I am not confident it will not cause failures on other items that I have not considered.
leakim971Multitechnician
Top Expert 2014

Commented:
encodeURIComponent is the way to go. Not sure why you get an error but I think you need to work on send_email.asp

you can't replace %0A because send_email don't "see" it. Try to replace with vbLF
Top Expert 2011

Author

Commented:
EncodeURIComponent fails too.

send_email.asp is code from GoDaddy (other than where I fetch the values)
<%
sendUrl="http://schemas.microsoft.com/cdo/configuration/sendusing"
smtpUrl="http://schemas.microsoft.com/cdo/configuration/smtpserver"

From = Request.Form("email")
ReplyTo = Request.Form("email")
SendTo = "myemailaddresshere"
Subject = Request.Form("subject")
Message = Replace(Request.Form("message"),"<br>",vbCRLF)

TextBody = "Message from: " & Request.Form("name") & vbCRLF & vbCRLF & "-------------" & vbCRLF & Message


' Set the mail server configuration
Set objConfig=CreateObject("CDO.Configuration")
objConfig.Fields.Item(sendUrl)=2 ' cdoSendUsingPort
objConfig.Fields.Item(smtpUrl)="relay-hosting.secureserver.net"
objConfig.Fields.Update


' Create and send the mail
Set objMail=CreateObject("CDO.Message")
' Use the config object created above
Set objMail.Configuration=objConfig
objMail.From=From
objMail.ReplyTo=ReplyTo
objMail.To=SendTo
objMail.Subject=Subject
objMail.TextBody=TextBody
objMail.Send
%>

Open in new window


What does ASP see "%0A" as?  I know vbCRLFs work OK, so maybe it is croaking on whatever %0A is interpreted as.
Multitechnician
Top Expert 2014
Commented:
yes encodeURIComponent encode more as you know, it was just to mention it (the same way you prefer encodeURI instead escape)

line 9, did you try :
Message = Replace(Request.Form("message"), vbLF, vbCRLF)

Open in new window

Top Expert 2011

Author

Commented:
Not yet. Will give it a try as soon as the babies are asleep.
Top Expert 2011

Author

Commented:
Thanks guys.

The fix took two parts:
1- needed to encode it before sending the data through the AJAX post
2- needed to replace the vbLF with vbCRLF on the ASP side before attempting to send the email.

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