TJF
TJF

Reputation: 1101

how do i pass js string variable to php without losing newlines and spaces?

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

Upvotes: 2

Views: 824

Answers (3)

peter
peter

Reputation: 1

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

Upvotes: 0

Bruno Silva
Bruno Silva

Reputation: 3097

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

Upvotes: 2

Sarfraz
Sarfraz

Reputation: 382776

I am not sure but try using encodeURIComponent function:

my_window.location = encodeURIComponent(urlString);

Upvotes: 1

Related Questions