JS/PHP Integration

A

ashore

Say I'm using a snippet like
$phpfoo = "a\nb\nc";
var jsfoo = "<?php echo $phpfoo;?>";

This errors out because JS sees the newline, and it needs to be
escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
retrieved and displayed OK.)

What do you folks use in this kind of situation? Thanks, all.

AS
 
P

Peter Michaux

Say I'm using a snippet like
$phpfoo = "a\nb\nc";
var jsfoo = "<?php echo $phpfoo;?>";

This errors out because JS sees the newline, and it needs to be
escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
retrieved and displayed OK.)

What do you folks use in this kind of situation? Thanks, all.

If the $phpfoo string in PHP contains a new line then this is
something you solve with PHP before you print it into the HTML page.
Turn the new line character into somthing that will print as "\n" into
the page if you want the JavaScript to see the new line.

Peter
 
M

Martin Honnen

ashore said:
Say I'm using a snippet like
$phpfoo = "a\nb\nc";
var jsfoo = "<?php echo $phpfoo;?>";

This errors out because JS sees the newline, and it needs to be
escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
retrieved and displayed OK.)

PHP has addslashes e.g.
var jsfoo = "<?php echo addslashes($phpfoo); ?>";
that should help (although it is there in PHP to deal with escaping
strings for data base stuff).
 
L

-Lost

ashore said:
Say I'm using a snippet like
$phpfoo = "a\nb\nc";
var jsfoo = "<?php echo $phpfoo;?>";

This errors out because JS sees the newline, and it needs to be
escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
retrieved and displayed OK.)

What do you folks use in this kind of situation? Thanks, all.

I imagine we would generate content that JavaScript could handle.

<script type="text/javascript">
var blah = '<?php print 'a\\nb\\nc'; ?>';
</script>

For example.
 
C

Christoph Burschka

Martin said:
PHP has addslashes e.g.
var jsfoo = "<?php echo addslashes($phpfoo); ?>";
that should help (although it is there in PHP to deal with escaping
strings for data base stuff).
Actually, it's not JS that turns "\n" into a newline, it's PHP.

If single quotes (like below) don't make it go away, *then* it's time to
look for ways to escape it on the Javascript side; not before.
 
G

gosha bine

Say I'm using a snippet like
$phpfoo = "a\nb\nc";
var jsfoo = "<?php echo $phpfoo;?>";

This errors out because JS sees the newline, and it needs to be
escaped somehow. (In my real case, $phpfoo came from MySQL, and it's
retrieved and displayed OK.)

What do you folks use in this kind of situation? Thanks, all.

AS

try

<?php echo addcslashes($phpfoo, "\0..\37");?>
 
L

-Lost

-Lost said:
I imagine we would generate content that JavaScript could handle.

<script type="text/javascript">
var blah = '<?php print 'a\\nb\\nc'; ?>';
</script>

For example.

Wait... you did not want the newlines, sorry.

\\\n or addslashes like Honnen suggested.
 
M

Martin Honnen

Martin said:
PHP has addslashes e.g.
var jsfoo = "<?php echo addslashes($phpfoo); ?>";
that should help

No, it does not help (as it does not esape \n), seems a regular
expression replacement is needed.
 
T

Toby A Inkster

Martin said:
PHP has addslashes e.g.
var jsfoo = "<?php echo addslashes($phpfoo); ?>";

In this case, addcslashes($phpfoo) would work better.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
 
A

ashore

Guys, the addslashes didn't work for me, tho I agree they shd! But
tell me why the following PHP didn't wrk:

$phpfoo = "\n\n\n";

print str_replace('\n' ,'ZZZZZZZZZZZZZ' ,$phpfoo);

AS
 
A

ashore

Hendri, you're right! I've been PHP'ing for years and didn't know
that. I knew in general that PHP wouldn't parse between sgl quotes,
but I didn't realize that included this situation. Thanks.

AS
 
A

ashore

Yep, that works fine. But I need to show line breaks in the html. so
the str_replace() is still there.

Thanks for all the help here, folks. 'Preciated!

AS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top