PDA

View Full Version : mySQL inside Javascript problem


gamoses
03-27-2002, 05:02 PM
I have a mySQL table with row titles and row descriptions. the idea is to have a list of titles with links to a javascript pop up box with full descriptions and other information. however, when the row description has certain html code (hyperlinks), it won't show the *title* correctly. naturally, the javascript doesn't work either. any ideas? here's the relevant code:

~ ~ ~ ~ ~

function PrintJavaScript() {
print "<!-- this java script takes the variables from an event shown below and opens a new window with the full information -->";
print "<script language=\"JavaScript\">\n";
print "<!--\n";
print "function desc_window(date,title,desc,time) {\n";
print " descWindow = window.open(\"\",\"EventDescription\",\"width=300,height=300,top=100,left=200,resizable=1,scrollbars=1,status=1\")\n";
print " descWindow.focus()\n";
print " descWindow.document.write('<HTML><HEAD><TITLE>'+title+'</TITLE></HEAD><BODY>')\n";
print " descWindow.document.write(\"<FONT face=arial size=2><B><big>\"+date+\"</big></B> <hr><b>\"+title+\"</b><br>\"+time+\"<br>\"+desc+\"<hr></BODY></HTML>\")\n";
print " autoClose = setTimeout(\"descWindow.close()\",30000)\n";
print "}\n";
print "-->\n";
print "</script>\n";
}


...


if ($desc) {
// pop up to description
$format_date = date ("l, F j, Y", mktime (0,0,0,$get_month,$get_day,$get_year));
$format_time = date ("g:i A", strtotime($this_time));
$format_title = addslashes($title);
$format_desc = addslashes($desc);
print "<a href=\"javascript:desc_window('$format_date','$format_title','$format_desc','$format_time')\">";
print stripslashes($title);
print "</a>";
}

~ ~ ~ ~ ~

fatal
04-01-2002, 01:00 PM
are you calling function PrintJavaScript() anywhere?? I dont know why you have that code inside php, you could just have it printed like regular HTML.

The reason why the javascript isnt working is b/c your never calling PrintJavascript so the javascript is never written.

If I were you, I wouldn't even have a function PrintJavaScript(), rather just the javascript itself.

gamoses
04-01-2002, 07:07 PM
Originally posted by fatal
are you calling function PrintJavaScript() anywhere?? I dont know why you have that code inside php, you could just have it printed like regular HTML.

The reason why the javascript isnt working is b/c your never calling PrintJavascript so the javascript is never written.

If I were you, I wouldn't even have a function PrintJavaScript(), rather just the javascript itself.


I do call the function at the top of the page. The PrintJavaScript is in an include file.

I guess my question has more to do with why the addslashes() isn't working. Or if that isn't it, why JavaScript is reading some of the backslashed characters.

top5mov
04-02-2002, 09:05 AM
outputting backslashed characters with PHP can be tricky, because PHP will try to interpret the backslashes instead of passing them along to javascript.

so PHP code like this:
$text = "He said, \"What's up?\"";
print "document.write(\"$text\")";

would print a javascript line like this:

document.write("He said, "What's up?"");

which will clearly cause a syntax error. so in the string that the javascript is going to print, you have to "backslash your backslash". like so:

$text = "He said, \\\"What's up?\\\"";
print "document.write(\"$text\")";

that should produce the output:

document.write("He said, \"What's up?\"");

i'd recommend using regular expressions to replace the \" sequences with \\\"