混合代码错误

时间:2014-05-09 16:23:52

标签: javascript php html

好的,我的网站上有一个基本的多功能一体页面,如下所示:

<?php

if($_GET['p']=='pb'){

echo '<p>pb</p>';

}elseif($_GET['p']=='example2'){

echo '<p>Example Two';

}elseif($_GET['p']=='example1'){

echo '<p>Example One';

}else{
echo '<h3>Page not defined&#59 error!</h3>';

}

?>

我遇到一些内联javascript的问题,当我尝试添加onclick =&#34; location.href =&#39;&#39; php混淆了它。我尝试过添加“&#39;在前面和后面,但它似乎不起作用,这就是我所拥有的:

<?php

if($_GET['p']=='pb'){

echo '<p>pb</p>';

}elseif($_GET['p']=='example2'){

echo '
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="location.href=\''/media/profiles/person/'\'">
<p>HOME</p>
</td>
</tr>
</table>';

}elseif($_GET['p']=='example1'){

echo '
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="location.href=\''/media/profiles/person/'\'">
<p>HOME</p>
</td>
</tr>
</table>';

}else{
echo '<h3>Page not defined&#59 error!</h3>';

}

?>

但我仍然会遇到一些错误,包括: - 使用未定义的常量媒体 - 假设&#39;媒体&#39; - 警告:除以零 -Notice:使用未定义的常量配置文件 - 假设&#39;配置文件&#39; -Notice:使用未定义的常数 - 假设的人#39; 等等。

我知道它与它有什么关系,但我该如何解决?

2 个答案:

答案 0 :(得分:1)

为什么不关闭PHP标签?

<?php

if($_GET['p']=='pb'){

echo '<p>pb</p>';

}elseif($_GET['p']=='example2'){

?>
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="javascript:location.href='/media/profiles/person/'">
<p>HOME</p>
</td>
</tr>
</table>
<?php

}elseif($_GET['p']=='example1'){

?>
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="javascript:location.href='/media/profiles/person/'">
<p>HOME</p>
</td>
</tr>
</table>
<?php

}else{
echo '<h3>Page not defined&#59 error!</h3>';

}

?>

此外,在您的应用程序中使用设计模式会更优雅 - 如果您坚持使用当前使用的“设计模式”,此应用程序将成为更新和维护的负担。

答案 1 :(得分:0)

你的报价错了。你正在逃避第一个单引号,但另一个跟随imediatly。第二个被php解释为字符串的结尾,这不是你想要的。只需删除第二个单引号即可修复错误。

echo '
<h1>Example Link One</h1>
<p>You clicked Example Link One!</p>
<table border="0" cellspacing="15" width="100%">
<tr>
<td bgcolor="grey" onclick="location.href=\'/media/profiles/person/\'">
<p>HOME</p>
</td>
</tr>
</table>';

编辑:另外值得一提:如果你有一个带有混合引号的字符串,通常更容易使用heredoc style

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
相关问题