By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,326 Members | 2,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,326 IT Pros & Developers. It's quick & easy.

String in php and js script

P: n/a
Hi everyone ..
i am trying to use a string which was saved by php form and contain
single quote ' or double quote

" .. when i using that string in my js script function like
function

onclick='js_test("<?=$string;?>")'

if $string contains double quote it will simply break the function ..
and same goes for single quote if i

surround $string with single quote i tried using escape and unescape
but that means .. every place i am using php ddata which might contain
single or double quote i have to use those javascript

function . as i am very new to programming if somebody could advise
what is the best way to deal

with string when we use js script and php

hope you can understand my english and hope somebody will reply soon

thanks
Dec 9 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
It's generally considered best practices to wrap HTML elements in
double quotes.
So, your onclick event handler should look like this:

onclick="js_test('<?php echo $string; ?>');"

(Note that it is also considered good form to use <?php ... ?to
denote PHP code at all times.)

Depending on the contents of $string, you might also want to use the
addslashes() function, like so...
onclick="js_test('<?php echo addslashes($string); ?>');"

Hope this helps.

On Dec 8, 9:51 pm, php_mysql_beginer911 <deepa...@gmail.comwrote:
Hi everyone ..
i am trying to use a string which was saved by php form and contain
single quote ' or double quote

" .. when i using that string in my js script function like
function

onclick='js_test("<?=$string;?>")'

if $string contains double quote it will simply break the function ..
and same goes for single quote if i

surround $string with single quote i tried using escape and unescape
but that means .. every place i am using php ddata which might contain
single or double quote i have to use those javascript

function . as i am very new to programming if somebody could advise
what is the best way to deal

with string when we use js script and php

hope you can understand my english and hope somebody will reply soon

thanks
Dec 9 '07 #2

P: n/a
..oO(st********@gmail.com)
>It's generally considered best practices to wrap HTML elements in
double quotes.
Attributes. And both single and double quotes are perfectly valid. In
this case it might make a difference because of the JS, but usually it
doesn't matter.

Micha
Dec 9 '07 #3

P: n/a
On Dec 9, 6:11 am, Michael Fesser <neti...@gmx.dewrote:
.oO(steve.h...@gmail.com)
It's generally considered best practices to wrap HTML elements in
double quotes.

Attributes. And both single and double quotes are perfectly valid. In
this case it might make a difference because of the JS, but usually it
doesn't matter.

Micha
XHTML allows you to use both single or double, but it's best to use
the double quotes since it's what most people expect.

Also, addslashes is perfect for this problem. Sounds like you'll need
to sanitize the string with htmlspecialchars.

My 2 cents,
Allain
Dec 9 '07 #4

P: n/a
..oO(allain)
>XHTML allows you to use both single or double, but it's best to use
the double quotes since it's what most people expect.
Who expects that?

Micha
Dec 10 '07 #5

P: n/a
On Mon, 10 Dec 2007 21:25:48 +0100, Michael Fesser <ne*****@gmx.dewrote:
.oO(allain)
>XHTML allows you to use both single or double, but it's best to use
the double quotes since it's what most people expect.

Who expects that?
Apparantly, some very, very bad parsers(1). And as XHTML is rendered as
tag soup most of the time anyway, and it's perfectly legal to use either
one, just take your pick. I just prefer double quotes because that's what
I'm used to, no other greater good in mind then ease of code :).

(1) http://worsethanfailure.com/Articles...ion-Error.aspx
--
Rik Wasmus
Dec 10 '07 #6

P: n/a
..oO(Rik Wasmus)
>On Mon, 10 Dec 2007 21:25:48 +0100, Michael Fesser <ne*****@gmx.dewrote:
>.oO(allain)
>>XHTML allows you to use both single or double, but it's best to use
the double quotes since it's what most people expect.

Who expects that?

Apparantly, some very, very bad parsers(1). And as XHTML is rendered as
tag soup most of the time anyway, and it's perfectly legal to use either
one, just take your pick. I just prefer double quotes because that's what
I'm used to, no other greater good in mind then ease of code :).
That's perfectly OK. I also usually use double quotes, but often enough
I use single quotes, if it makes my PHP code more readable:

print "<img src='$someVar' alt='...'>\n";

vs.

print "<img src=\"$someVar\" alt=\"...\">\n";

or

print '<img src="'.$someVar.'" alt="...">'."\n";

I definitely prefer the first one. Every browser accepts it and for the
website user it doesn't matter at all.

Micha
Dec 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.