473,387 Members | 1,812 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,387 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
14 1386
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
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 #7
Martin Honnen wrote:
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.
Huh? addslashes would yield a\\nb\\nc. That is effectively escaping
newlines is it not?

--
-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 #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
-Lost wrote:
Huh? addslashes would yield a\\nb\\nc. That is effectively escaping
newlines is it not?
In the given example:

$phpfoo = "a\nb\nc";

No, it would not. addslashes() wouldn't do anything to that string.

--
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 #10
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 #11
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 #12
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 #13
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 #14
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 #15

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.