2

i have a javascript variable which contains a string with new lines and spaces e.g "a \n d" i want to pass this to php without losing any spaces or new lines. currently i am using this:

my_window = window.open("", "ChemEdit Molfile", "status=1,width=550,height=350");    
urlString = "/chemedit/b.php?var=" +r;
my_window.location = urlString;

where r is the string i pass.

but if i do this in php

echo $_GET["var"];

i just get it on one line with the spaces gone

please help

1
  • Have you tried to escape the r variable? Commented Mar 11, 2012 at 17:57

3 Answers 3

2

You need to encode the string, you can use encodeURIcomponent() for that.

Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure but try using encodeURIComponent function:

my_window.location = encodeURIComponent(urlString);

1 Comment

+1 but more like urlString = "/chemedit/b.php?var=" + encodeURIComponent(r);
0

if you're going to pass newline and spaces in an url, you have to abide by the character rules of url strings, in which spaces must be represented as '%20' and newlines '\n" is '%5Cn'. the answers above will probably do this automatically, but if you find you ever need to manually encode or adjust, here is a reference for all special ascii characters that should be converted for passing through an http header request: http://www.w3schools.com/tags/ref_urlencode.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.