PHP代码:--------------------------------------------------------------------------------
<?php require_once(Connections/kalika.php); ?>
<?php
mysql_select_db($database_kalika, $kalika);
$query_news = "SELECT klk_topics.title,klk_posts.post,klk_posts.pid,klk_topics.description FROM klk_posts, klk_topics WHERE klk_posts.forum_id=4 and klk_posts.topic_id=klk_topics.tid ";
if ($_GET[news_id])
{
$news_id = intval($_GET[news_id]);
$query_news .= " and klk_posts.pid = $news_id";
}
else
{
$query_news .= " limit 0, 1";
}
$news = mysql_query($query_news, $kalika) or die(mysql_error());
$row_news_title = mysql_fetch_assoc($news);
$totalRows_news_title = mysql_num_rows($news);
$query_more_news = "SELECT klk_topics.title,klk_posts.post,klk_posts.pid,klk_topics.description FROM klk_posts, klk_topics WHERE klk_posts.forum_id=4 and klk_posts.topic_id=klk_topics.tid limit 0, 8";
$more_news = mysql_query($query_more_news, $kalika) or die(mysql_error());
$row_more_news_title = mysql_fetch_assoc($more_news);
$totalRows_more_news_title = mysql_num_rows($more_news);
?>
...
<TD height="101" align=middle vAlign=top bgcolor="#ECE3E3">
<script language=JavaScript>
<!--
var index = 8
link = new Array(8)
text = new Array(8)
<?php
$i = 0;
do
{
$k = $i + 1;
echo "link[$i] = " . htmlspecialchars("zhongxin.php?&news_id=" . urlencode($row_more_news_title[pid])) ;
echo "text[$i] = $k." . $row_more_news_title[title] ;
$i++;
}
while ($row_more_news_title);
echo "
document.write (\"<marquee scrollamount=1 scrolldelay=100 direction= up width=160 height=150>\")
for (i=0;i<index;i++){
document.write (\" <a href=\"+link[i]+\" target=_parent>\")
document.write (text[i] + \"</A><br>\")
}
document.write (\"</marquee>\")";
?>
-->
</script>;
</TD>
</TR>
///////////////////////////////////////////////
不好意思。代码太杂。
我想问题主要出在echo js代码过程中是不是有些该转义的字符没有转义,运行时报错的都是echo js代码的行。
第一次用php结合js,请大家指教。
这里主要是要把一个php数组传给一个js数组,大家有什么经验的说。
注意,当这些输出出现在js部分时,必须符合js语法也无须用htmlspecialchars转义
echo "link[$i] = " . htmlspecialchars("zhongxin.php?&news_id=" . urlencode($row_more_news_title[pid])) ;
echo "text[$i] = $k." . $row_more_news_title[title] ;
$i++;
输出结果应形如
link[1] = "zhongxin.php?&news_id=xxxxxx";
text[1] = "title";
显然你的代码没有满足这个要求
1、串类型数据没有用引号括起
2、缺少行结束符“;”或“\n”