2016-07-07 19:28
浏览 81

无法在<script>标签内回显PHP变量? [关闭]

This is my PHP code. I want error to be displayed using an alert window.

 if ($projectid=="")
    $error = 'You must choose a project.
              Click &lsquo;back&rsquo; and try again.';
    include error.html.php';

This is error.html.php that is supposed to be parsed by the browser

<!DOCTYPE html>
<html lang="en">
        <meta charset="utf-8">
        <title>Script Error</title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">



             var error = <?php echo   $error ; ?>;
              window.onload = function(){ alert(error); }

Wahts up with the script tags ? Do they prevent PHP from running ?

图片转代码服务由CSDN问答提供 功能建议

这是我的PHP代码。 我希望使用警报窗口显示错误。

  if($ projectid ==“”)
 $ error ='您必须选择一个项目。\  n点击&amp; lsquo; back&amp; rsquo; 然后再试一次。'; 

这是error.html.php 应该由浏览器解析

 &lt;!DOCTYPE html&gt; 
&lt; html lang =“en”&gt; 
&lt; head&gt; 
&lt; meta  charset =“utf-8”&gt; 
&lt; title&gt;脚本错误&lt; / title&gt; 
&lt; meta charset =“utf-8”&gt; 
&lt; meta http-equiv =“X-UA-Compatible  “content =”IE = edge“&gt; 
&lt; meta name =”viewport“content =”width = device-width,initial-scale = 1“&gt; 
&lt; / head&gt; 
&lt; body&gt; 
&lt; script&gt;  
 var error =&lt;?php echo $ error;  ?&gt ;; 
 window.onload = function(){alert(error);  } 
&lt; / script&gt; 
&lt; / body&gt; 
&lt; / html&gt; 

Wahts up脚本标签? 它们会阻止PHP运行吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • douyu9012 2016-07-07 20:20

    The real problem is that this is what your rendered result looks like:

    var error = You must choose a project.
                Click &lsquo;back&rsquo; and try again.

    Does that looks like valid JavaScript to you? I think not.

    var error = <?=json_encode($error);?>;

    That should result in:

    var error = "You must choose a project.
                     Click &lsquo;back&rsquo; and try again.";

    Much better.

    解决 无用
    打赏 举报

相关推荐 更多相似问题