Send multiline PHP variable to javascript

Status
Not open for further replies.

last2kn0

New Member
Messages
10
Reaction score
0
Points
1
I would like to send a multiple line php string to a javascript variable but I am unaware how.

I know that javascript multiline strings must be escaped like this:

var string = "this multi \
line string";

I am currently sending php variables to javascript like this:
<?php echo "<script>var string=$phpstring</script>

Also, I bet a big part of this is that this string is PKCS7 encrypted. So I guess the format must be strict? I very new to this and I'm making some assumptions.

This however doesn't allow for multiline assignments. How would I accomplish this?

Thanks in advance!
 
Last edited:

blu3fire

New Member
Messages
14
Reaction score
0
Points
0
Why do you need multiline? In PKCS#7 you cannot use any white space characters (newline character too). BTW can't you make each line as new variable or an element of array?
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Try using:

PHP:
$string = str_replace("\n", "\\n", $string);
echo "<script>var string='$string';</script>";

This should translate newlines to the literal escape characters which JS should then translate back to newlines.
 

last2kn0

New Member
Messages
10
Reaction score
0
Points
1
Well I am unable to determine where the initial problem extends from but the first time I attempted to give javascript a single line version of the encrypted blob, the paypal service couldn't decrypt it.

During debugging this problem, some of my debugging code was preventing me from seeing a solution (I was using getElementsByName("encrypt")[0] and referincing the wrong index as there were multiple fields with the name encrypt).

Anyways, giving javascript a single line encryption blob does work. If anyone needs more specifics on the solution, feel free to inquire and I will give you any details.
 
Status
Not open for further replies.
Top