Escaping

Hello,

I am trying to output HTML and JavaScript using PHP as the following:

<span style="float:right;">
<input type="button" value="Edit" onclick="startEditCategory('<?php
echo("{$categoryId}"); ?>', '<?php echo("{$categoryName}"); ?>');" />
</span>

The $categoryName variable can contain single and double quotes, so
how can I escape them?

Thanks.
ahmad [ Do, 10 April 2008 21:44 ] [ ID #1940702 ]

Re: Escaping

On 10 Apr, 20:44, Ahmad <ah... [at] medicfusion.com> wrote:
> Hello,
>
> I am trying to output HTML and JavaScript using PHP as the following:
>
> <span style="float:right;">
> <input type="button" value="Edit" onclick="startEditCategory('<?php
> echo("{$categoryId}"); ?>', '<?php echo("{$categoryName}"); ?>');" />
> </span>
>
> The $categoryName variable can contain single and double quotes, so
> how can I escape them?
>

Also losing the syntactic overkill:

<input type="button" value="Edit" onclick="startEditCategory('<?php
echo htmlentities($categoryId); ?>', '<?php echo
htmlentities($categoryName); ?>');" />

C.
colin.mckinnon [ Do, 10 April 2008 22:14 ] [ ID #1940705 ]

Re: Escaping

On Apr 10, 11:14 pm, "C. (http://symcbean.blogspot.com/)"
<colin.mckin... [at] gmail.com> wrote:
> On 10 Apr, 20:44, Ahmad <ah... [at] medicfusion.com> wrote:
>
> > Hello,
>
> > I am trying to output HTML and JavaScript using PHP as the following:
>
> > <span style="float:right;">
> > <input type="button" value="Edit" onclick="startEditCategory('<?php
> > echo("{$categoryId}"); ?>', '<?php echo("{$categoryName}"); ?>');" />
> > </span>
>
> > The $categoryName variable can contain single and double quotes, so
> > how can I escape them?
>
> Also losing the syntactic overkill:
>
> <input type="button" value="Edit" onclick="startEditCategory('<?php
> echo htmlentities($categoryId); ?>', '<?php echo
> htmlentities($categoryName); ?>');" />
>
> C.

That is right but I don't want to convert all HTML special characters
and how to convert the variables back on the javascript function?
ahmad [ Do, 10 April 2008 22:48 ] [ ID #1940709 ]

Re: Escaping

Ahmad escribió:
> <span style="float:right;">
> <input type="button" value="Edit" onclick="startEditCategory('<?php
> echo("{$categoryId}"); ?>', '<?php echo("{$categoryName}"); ?>');" />
> </span>
>
> The $categoryName variable can contain single and double quotes, so
> how can I escape them?

With htmlspecialchars().

BTW, do you actually know what "{$categoryId}" does? You provide an
array and tell PHP to parse it to find variables and replace them with
their values. That could make sense with stuff like "Hello, $name\n" but
not with an array that just contains a variable.



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
alvaroNOSPAMTHANKS [ Fr, 11 April 2008 09:29 ] [ ID #1940760 ]
PHP » comp.lang.php » Escaping

Vorheriges Thema: how to use php to control webform? (computer identification)
Nächstes Thema: A little help with isset, in_array