473,789 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String in php and js script

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_tes t("<?=$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
6 1515
It's generally considered best practices to wrap HTML elements in
double quotes.
So, your onclick event handler should look like this:

onclick="js_tes t('<?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_tes t('<?php echo addslashes($str ing); ?>');"

Hope this helps.

On Dec 8, 9:51 pm, php_mysql_begin er911 <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_tes t("<?=$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
..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
On Dec 9, 6:11 am, Michael Fesser <neti...@gmx.de wrote:
.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 htmlspecialchar s.

My 2 cents,
Allain
Dec 9 '07 #4
..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
On Mon, 10 Dec 2007 21:25:48 +0100, Michael Fesser <ne*****@gmx.de wrote:
.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
..oO(Rik Wasmus)
>On Mon, 10 Dec 2007 21:25:48 +0100, Michael Fesser <ne*****@gmx.de wrote:
>.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4902
by: DeepBleu | last post by:
When one is using an HTML form via a web broswer, the user submits the form contents and these are passed to a CGI Python script on the web server. I need to write a client script that connects to a web site, runs a Python CGI script on the web server AND passes a string to the CGI Python script that the Python CGI script can store/manipulate/evalute etc.... I know how to connect to the web site and run the CGI Python script using a client...
0
1770
by: John F Dutcher | last post by:
Having 'cloned' an existing python script that imports 'string' and uses "string.rstrip()" without incident... I am at a loss to explain why the new 'cloned' script (brief sample below) continually errors with: NameError: global name 'string' is not defined args = ("global name 'string' is not defined",) when run from the server CGI-BIN. If the same script is run in 'IDLE' it
4
4817
by: news-server.tampabay.rr.com | last post by:
Hi, Below is a stock script I found which controls a framed environment. My problem is that if a URL has a query string attached, that string does not pass through. Can someone please let me know if and hopefully how, it will be possible to carry a query string through? This first part here is in the default.asp framed page. <SCRIPT LANGUAGE="JavaScript">
4
3182
by: lkrubner | last post by:
I'd like to write a PHP script to be used from the command line on a Unix machine. I'd like for the script to put together a string, turn it into a web page, print it, then return control the command line. Obviously it is quite easy to make a string that is valid HTML and send it to the printer. The tough part is making look like a web page. In particular, image tags should print out as images, not image tags. Does anyone have an idea...
6
2128
by: tshad | last post by:
In my User control, I tried to do this: *************************************************************************** <Script runat="server"> Public ClientName As String = "<!-- #include file = ...\includes\StaffingHeaders.inc -->" </Script> <%=ClientName%> ****************************************************************************
10
1996
by: Alan Silver | last post by:
Hello, I have noticed in a few bits of code that people sometimes do things like... SomeFunction(@"a text string"); What is the purpose of the @ sign? I have seen it most when passing connection strings (admittedly in example code, as connection strings would be better stored elsewhere in a real app), but I don't think it's
18
2270
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I then want to use the string on the same page. My script is: function checkRadio(field) { for(var i=0; i < field.length; i++) {
3
2007
by: Alex | last post by:
Hello. First, with AJAX I will get a remote web page into a string. Thus, a string will contain HTML tags and such. I will need to extract text from one <span> for which I know the ID the inner text. Is it possible to access in this way "string variable".getElementByID() somehow? Thank you.
4
2332
by: shapper | last post by:
Hello, I am creating a string using StringBuilder: Dim code As New StringBuilder code.Append("<script type=""text/javascript""><!--") Response.Write(code.ToString) However, I don't get any code when I do Response.Write.
3
240
by: shapper | last post by:
Hello, I have an Action where I define a ViewData property named Ad: public ActionResult GoogleAdSense(string client, int height, string slot, int width) { StringBuilder ad = new StringBuilder(); ad.AppendLine("<script type='text/javascript'>"); ad.AppendLine("<!--"); ad.AppendLine(String.Concat("google_ad_client = '", client,
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9499
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10121
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8995
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5539
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4076
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.