...

View Full Version : mixture of php and javascript ... link



jeromepelletier
06-23-2006, 08:31 PM
Hello, i am trying to get a link that calls a JS function im using

$calltext = "<a onclick=myJSFunc('$someRandomphpVar'); return false href=mydoc.php>Click Me!!</a>";

when you click the link, the page changes to mydoc.php, any ideas on how to stop this (the function of course works XD)

thanks

fci
06-23-2006, 08:36 PM
try:

$calltext = '<a onclick="myJSFunc(\''.addslashes($someRandomphpVar).'\'); return false;" href="mydoc.php">Click Me!!</a>';

jeromepelletier
06-23-2006, 08:48 PM
that gives a error ... error expected ';'

fci
06-23-2006, 08:55 PM
then you either copy and pasted wrong or there is an error somewhere else in your code.

jeromepelletier
06-23-2006, 09:13 PM
how can you c/p wrong? lol

fci
06-23-2006, 09:29 PM
the error says you're missing a semi-colon, maybe you missed that when you copy/pasted.

jeromepelletier
06-23-2006, 09:31 PM
i didnt..i even c/p it a couple of times and its not my code..because it works when i used what id di form my firts post

fci
06-23-2006, 09:34 PM
then post your code around the area the error comes from.

jeromepelletier
06-23-2006, 09:35 PM
.. the are is the code you gave me lol

fci
06-23-2006, 09:38 PM
.. the are is the code you gave me lol

copy and paste whatever it is so I can see the error.

jeromepelletier
06-23-2006, 09:41 PM
$calltext = '<a onclick="myJSFunc(\''.addslashes($someRandomphpVar).'\'); return false;" href="mydoc.php">Click Me!!</a>'; ....

fci
06-23-2006, 09:46 PM
and if you comment it out you don't receive the error?

jeromepelletier
06-23-2006, 09:49 PM
nope, page works fine once i take that out

fci
06-23-2006, 09:56 PM
if this doesn't work then I give up, this shouldn't be so complicated:

$calltext = "<a onclick=\"myJSFunc('".addslashes($someRandomphpVar)."'); return false;\" href=\"mydoc.php\">Click Me!!</a>";

GJay
06-23-2006, 10:38 PM
There was nothing wrong with the code you first posted...

fci
06-23-2006, 11:04 PM
There was nothing wrong with the code you first posted...

I know.

jeromepelletier
06-24-2006, 07:36 PM
if this doesn't work then I give up, this shouldn't be so complicated:

$calltext = "<a onclick=\"myJSFunc('".addslashes($someRandomphpVar)."'); return false;\" href=\"mydoc.php\">Click Me!!</a>";
I still get an error with this :S I have no clue why it works without it and with it it gives an error :S


Edit:

I think it might be the quotes, if you view source the code you will find:

parent.frames['frame'].div="<a onclick="myJ

could that be it ?

fci
06-24-2006, 09:08 PM
well, the JS won't work, that's for sure. you will need to escape the double quotes within the html.

jeromepelletier
06-24-2006, 09:24 PM
uhmmm yeah................no clue how to

fci
06-24-2006, 09:37 PM
this a normal double quote:
"

this is an escape double quote:
\"

Kurashu
06-24-2006, 09:51 PM
The easiest way to escape double quotes in PHP is to not use them, at least where html and the like is involved. However, here you'll be escaping some kind of quotes no matter what.


$calltext = '<a onclick="myJSFunc(\''.addslashes($someRandomphpVar).'\'); return false;" href="mydoc.php">Click Me!!</a>';

That's another way to code it, but you'd have to be careful to make sure you escape the right single quotes.

jeromepelletier
06-24-2006, 10:56 PM
The easiest way to escape double quotes in PHP is to not use them, at least where html and the like is involved. However, here you'll be escaping some kind of quotes no matter what.


$calltext = '<a onclick="myJSFunc(\''.addslashes($someRandomphpVar).'\'); return false;" href="mydoc.php">Click Me!!</a>';

That's another way to code it, but you'd have to be careful to make sure you escape the right single quotes.

That didnt work either :/

Could my problem lie in where I am converting a php var to javascript?


print('var someJSVAR = "' . $calltext.'"');

fci
06-25-2006, 01:58 AM
try addslashes($calltext)

and, why did you not originally say the error was coming from the javascript. I can't read your mind.

jeromepelletier
06-25-2006, 04:00 AM
i thought i did say it was javascript, my bad.

jeromepelletier
06-26-2006, 02:38 AM
try addslashes($calltext)

and, why did you not originally say the error was coming from the javascript. I can't read your mind.
That didnt work either

jeromepelletier
06-27-2006, 10:35 PM
if I do


$calltext = '<a onclick="myJSFunc($ddsds); return false;" href="mydoc.php">Click Me!!</a>';
addslashes($calltext);

the script doesnt break, but when you click the link, the page goes to that location

fci
06-27-2006, 10:50 PM
addslashes doesn't pass by reference
you need to do:
$calltext = addslashes($calltext);

chump2877
06-27-2006, 11:08 PM
if I do


$calltext = '<a onclick="myJSFunc($ddsds); return false;" href="mydoc.php">Click Me!!</a>';
addslashes($calltext);

the script doesnt break, but when you click the link, the page goes to that location


If you don;t want the link to forward you to mydoc.php, you could try/do a couple of things:


$calltext = '<a onclick="myJSFunc($ddsds); return false;" href="#">Click Me!!</a>';
$calltext = addslashes($calltext);

$calltext = '<a href="javascript: myJSFunc($ddsds); return false;">Click Me!!</a>';
$calltext = addslashes($calltext);

$calltext = '<a onclick="myJSFunc($ddsds); return false;" href="javascript: void(0)">Click Me!!</a>';
$calltext = addslashes($calltext);

As long as you aren;t redirecting in your javascript function, one of those has to work...

jeromepelletier
06-27-2006, 11:23 PM
If you don;t want the link to forward you to mydoc.php, you could try/do a couple of things:


$calltext = '<a onclick="myJSFunc($ddsds); return false;" href="#">Click Me!!</a>';
$calltext = addslashes($calltext);

$calltext = '<a href="javascript: myJSFunc($ddsds); return false;">Click Me!!</a>';
$calltext = addslashes($calltext);

$calltext = '<a onclick="myJSFunc($ddsds); return false;" href="javascript: void(0)">Click Me!!</a>';
$calltext = addslashes($calltext);

As long as you aren;t redirecting in your javascript function, one of those has to work...
i thought useing href="javascript: void(0)"> or href="#"> was a bad idea (from using google)

chump2877
06-27-2006, 11:36 PM
i thought useing href="javascript: void(0)"> or href="#"> was a bad idea (from using google)

There's nothing wrong with using those things if the link is only for calling a javascript function....

If you are worried that you have a link embedded in your Javascript function, yes, that is a problem with search engines, because search engines have a hard time spidering links inside of javascript...

If you need an HTML link to mydoc.php, you might consider creating a separate HTML link just for the purpose of seach engine indexing (this is why many people create "link maps" for their sites -- so search engines like Goggle can easily spider all of the links on their site via a list of easy-to-follow HTML links)

jeromepelletier
06-27-2006, 11:47 PM
nope, the link is only to call a function :)

jeromepelletier
06-28-2006, 12:35 AM
those 3 links all gave me a undefined msg, such as, '$ddsds is undefined'

chump2877
06-28-2006, 12:56 AM
The error is in your javascript function then....and make sure $ddsds has a value...

Post the javascript here or in the javascript forum and someone will help you....


Edit: On second glance, you need to concatenate the PHP variable:


$calltext = '<a onclick="myJSFunc('.$ddsds.'); return false;" href="javascript: void(0)">Click Me!!</a>';
$calltext = addslashes($calltext);

jeromepelletier
06-28-2006, 01:24 AM
The error is in your javascript function then....and make sure $ddsds has a value...

Post the javascript here or in the javascript forum and someone will help you....
now we are getting somewhere :) function is halfway doing what i want... just that, whatever the php variable is, the error now says what it is, like i set it to "blah" and the javascript says "blah is undefined",


parent.document.getElementById('myDiv').innerHTML = '<? print($calltext) ?>';

that there, i am putting the php into a Div on the top of the page, then in the function i am doing


function someJSfunc(theVar)
{

var submitERName = theVar;
parent.myFrame.document.getElementById("myFormthing").value="/pm " + submitERName + ": ";

parent.myFrame.document.getElementById("myFormthing").focus();
parent.myFrame.document.getElementById("myFormthing").value=parent.myFrame.document.getElementById("myFormthing").value.replace(/x$/,"");

}

and the php, i am just using the link you provided me with latest in your edit,


$calltext = '<a onclick="myJSFunc('.$ddsds.'); return false;" href="javascript: void(0)">Click Me!!</a>';
$calltext = addslashes($calltext);
and i set ddsds to blah



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum