echo javascript syntax

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
 Could someone show me how to echo/print the following:

<a style="color: blue;" href="javascript:someFunction('someFile.php?
someParam=<?php $someParam ?>')">someLabel</a>


Re: echo javascript syntax

student4life wrote:
Quoted text here. Click to load it

What's the problem with just what you have (outside of the <?php ... ?>
tags, of course)?

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: echo javascript syntax

Jerry Stuckle wrote:
Quoted text here. Click to load it

print '<a style="color: blue;"
          href="javascript:someFunction(' . "'someFile.php?
          someParam=<?php $someParam ?>'" . ')">someLabel</a>';

Re: echo javascript syntax

On Jul 6, 6:17=A0pm, sheldonlg <sheldonlg> wrote:
Quoted text here. Click to load it

I'd go with escaping the quotes inside the string:

print "<a style=3D\"color: blue;\"


Re: echo javascript syntax

student4life schrieb:
Quoted text here. Click to load it

Why do you want that? It's a good practice to separate between you
application and presentation logic and your fragment belongs to the
latter. If you use print or echo to output HTML, it's hard to read and
maintain later on.

Quoted text here. Click to load it
You forgot a "echo". But you shouldn't use the "href" attribute at all,
because user-agents without JavaScript support encounter a broken link.

I would wrote

<a style="color:blue"
     href="someFile.php?someParam=<?php echo urlencode($someParam) ?>"
  onclick="return someFunction(this.href)">label</a>

and someFunction() should then return "false" when it was successful to
cancel the onclick event. Nothing more. If you still want to print it
directly in the code you can use:

     '<a style="color:blue"
  onclick="return someFunction(this.href)">label</a>',



echo '<a style="color:blue" href="someFile.php?someParam=',
   ' onclick="return someFunction(this.href)">label</a>';

If you don't want to use "this.href" (or even worse, stay with your
approach) you have to escape the single quotes of the JavaScript method

echo '<a onclick="foo(\'bar\'); return false">foobar</a>';
                       ^^   ^^

Re: echo javascript syntax


Quoted text here. Click to load it

This is way easy if I'm reading the question correctly.

<a style="color: blue;" href="javascript:someFunction('someFile.php?
someParam='<?php echo "$someParam"; ?>')">someLabel</a>

I didn't really parse the whole thing.  If you need a value from a PHP variable,
just write:

<?php echo "$variable"; ?>

In fact, usually you can omit the " " around the $variable.

Re: echo javascript syntax

Mason Barge wrote:
Quoted text here. Click to load it

He was concerned, I believe, about the single and double quotes.  I
showed him one way to handle these (otherwise it really is a trivial
question).  Another is by putting in backslashes.

Re: echo javascript syntax

Quoted text here. Click to load it


Quoted text here. Click to load it

In fact, it's usually a bad idea to unnecessarily use double quotes
when dealing with single variables as arguments/operands.  In the
context of outputting data, it is most likely harmless, but if used
in contexts like assignments or function arguments, it can cause some
issues with types that might not be obvious to beginner PHP

  handleError("$errorCode"); /* $errorCode is a string, not an int */

If the `handleError()' function were explicitly expecting its
parameter to be an integer, there could be a problem.

~Curtis Dyer
<?php $x='<?php $x=%c%s%c;%cprintf($x,39,$x,39,10,10); ?>%c';
printf($x,39,$x,39,10,10); ?>

Site Timeline