473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

JS/PHP Integration

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

Apr 27 '07 #1
13 1351
On Apr 27, 7:35 am, ashore <3ash...@comcast.netwrote:
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

Apr 27 '07 #2
ashore wrote:
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).
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 27 '07 #3
ashore wrote:
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.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Apr 27 '07 #4
Martin Honnen schrieb:
ashore wrote:
>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).

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.
> <?php $phpfoo = 'a\nb\nc'; ?>
var jsfoo = "<?php echo $phpfoo;?>";
--
cb
Apr 27 '07 #5
On 27.04.2007 16:35 ashore wrote:
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");?>
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Apr 27 '07 #6
-Lost wrote:
ashore wrote:
>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.
Wait... you did not want the newlines, sorry.

\\\n or addslashes like Honnen suggested.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Apr 27 '07 #7
Martin Honnen wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 27 '07 #8
Martin Honnen wrote:
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!
Apr 27 '07 #9
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

Apr 27 '07 #10
ashore wrote:
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
Because of the single quote.
PHP process "\n", but does not process '\n';

Hendri
Apr 27 '07 #11
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

Apr 27 '07 #12
ashore wrote:
print str_replace('\n' ,'ZZZZZZZZZZZZZ' ,$phpfoo);
Stop mucking around with str_replace. Just use addcslashes().

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

* = I'm getting there!
Apr 28 '07 #13
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

Apr 28 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wolfgang Keller | last post by:
Hello, Python seems to be used quite a lot for (the integration of) enterprise applications. Just as an example, there are at least three (projects for the implementation of) ERP systems in...
4
by: Dan | last post by:
Hello all. I am currently working on a project for several Hospitals. The application is written in Java, and the database is either Oracle or MySql, depending on the client. For a while now,...
0
by: Wayan | last post by:
Geekcorps Volunteer - Systems Integration Kenya, East Africa Geekcorps http://www.geekcorps.org is in search of systems integration professionals experienced in developing communication systems...
0
by: Magic1812 | last post by:
Magic Software invites you to join us this coming Tuesday (January 13th, 2004) at 12:00 EDT / 17:00 GMT for a FREE live Webinar: Title: Accelerated Application Integration and Information...
0
by: Magic1812 | last post by:
Magic Software invites you to join us this coming Tuesday (January 13th, 2004) at 12:00 EDT / 17:00 GMT for a FREE live Webinar: Title: Accelerated Application Integration and Information...
0
by: S Arnold | last post by:
Date: June 24, 2004 Time: 4:00 PM - 5:00 PM (GMT +01:00) Presenter: Richard Curtis Description: iBOLT, Magic's comprehensive EAI and BPM suite was recently described by VP Strategic Services...
0
by: Stylus Studio | last post by:
DataDirect XQuery(TM) is the First Embeddable Component for XQuery That is Modeled after the XQuery API for Java(TM) (XQJ) BEDFORD, Mass.--Sept. 20, 2005--DataDirect Technologies...
3
by: gb | last post by:
We are in the process of upgrading part of a large system to .NET, whilst the majority will remain ASP. Sharing session state information will not be a problem at the moment as it is trivial and...
0
by: Stylus Studio | last post by:
Hey Everyone, A new podcast entitled: Business-to-Business Data Integration in a SOA World was just released by ZapThink. The key speakers in the podcast are the following: ZapThink...
1
by: YellowfinTeam | last post by:
Marketplace: Yellowfin reporting 3.1 with BIRT Integration Yellowfin is proud to announce the release of 3.1. The major theme of this release is enhanced integration capability. We have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.