jeromepelletier 06-23-2006, 07: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
try:
$calltext = '<a onclick="myJSFunc(\''.addslashes($someRandomphpVar).'\'); return false;" href="mydoc.php">Click Me!!</a>';
jeromepelletier 06-23-2006, 07:48 PM that gives a error ... error expected ';'
then you either copy and pasted wrong or there is an error somewhere else in your code.
jeromepelletier 06-23-2006, 08:13 PM how can you c/p wrong? lol
the error says you're missing a semi-colon, maybe you missed that when you copy/pasted.
jeromepelletier 06-23-2006, 08: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
then post your code around the area the error comes from.
jeromepelletier 06-23-2006, 08:35 PM .. the are is the code you gave me lol
.. 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, 08:41 PM $calltext = '<a onclick="myJSFunc(\''.addslashes($someRandomphpVar).'\'); return false;" href="mydoc.php">Click Me!!</a>'; ....
and if you comment it out you don't receive the error?
jeromepelletier 06-23-2006, 08:49 PM nope, page works fine once i take that out
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>";
There was nothing wrong with the code you first posted...
There was nothing wrong with the code you first posted...
I know.
jeromepelletier 06-24-2006, 06: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 ?
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, 08:24 PM uhmmm yeah................no clue how to
this a normal double quote:
"
this is an escape double quote:
\"
Kurashu 06-24-2006, 08: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, 09: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.'"');
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, 03:00 AM i thought i did say it was javascript, my bad.
jeromepelletier 06-26-2006, 01: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, 09: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
addslashes doesn't pass by reference
you need to do:
$calltext = addslashes($calltext);
chump2877 06-27-2006, 10: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, 10: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, 10: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, 10:47 PM nope, the link is only to call a function :)
jeromepelletier 06-27-2006, 11:35 PM those 3 links all gave me a undefined msg, such as, '$ddsds is undefined'
chump2877 06-27-2006, 11:56 PM 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, 12: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
|