【问题标题】:How to execute a PHP page with HTML in it?如何执行包含 HTML 的 PHP 页面?
【发布时间】:2016-11-09 06:06:17
【问题描述】:

我正在尝试使用 php 创建一个在线评论系统。这是我的 php 页面,其中包含 HTML。但是,它没有按预期执行,呈现 HTML 元素,而 php 在执行期间作为垃圾返回。我错过了什么吗?我是否使用 .htaccess 文件告诉编译器将其作为纯粹的 php 文件执行?

Error image

这是我的代码:

<?php
if (version_compare(phpversion(), "5.3.0", ">=")  == 1)
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
else
error_reporting(E_ALL & ~E_NOTICE); 

require_once('classes/CMySQL.php'); 

$sCode = '';
$iItemId = (int)$_GET['id'];
if ($iItemId) { // View item output
$aItemInfo = $GLOBALS['MySQL']->getRow("SELECT * FROM `s163_items` WHERE `id` = '{$iItemId}'"); // getting info about item from database
$sCode .= '<h1>'.$aItemInfo['title'].'</h1>';
$sCode .= '<h3>'.date('F j, Y', $aItemInfo['when']).'</h3>';
$sCode .= '<h2>Description:</h2>';
$sCode .= '<h3>'.$aItemInfo['description'].'</h3>';
$sCode .= '<h3><a href="'.$_SERVER['PHP_SELF'].'">back</a></h3>';


$sComments = '';
$aComments = $GLOBALS['MySQL']->getAll("SELECT * FROM `s163_items_cmts`     WHERE `c_item_id` = '{$iItemId}' ORDER BY `c_when` DESC LIMIT 5");
foreach ($aComments as $i => $aCmtsInfo) {
    $sWhen = date('F j, Y H:i', $aCmtsInfo['c_when']);
    $sComments .= <<<EOF
<div class="comment" id="{$aCmtsInfo['c_id']}">
<p>Comment from {$aCmtsInfo['c_name']} <span>({$sWhen})</span>:</p>
<p>{$aCmtsInfo['c_text']}</p>
</div>
EOF;
}

ob_start();
?>
<div class="container" id="comments">
    <h2>Comments</h2>
    <script type="text/javascript">
    function submitComment(e) {
        var sName = $('#name').val();
        var sText = $('#text').val();

        if (sName && sText) {
            $.post('comment.php', { name: sName, text: sText, id: <?=  $iItemId ?> }, 
                function(data){ 
                    if (data != '1') {
                      $('#comments_list').fadeOut(1000, function () { 
                        $(this).html(data);
                        $(this).fadeIn(1000); 
                      }); 
                    } else {
                      $('#comments_warning2').fadeIn(1000, function () { 
                        $(this).fadeOut(1000); 
                      }); 
                    }
                }
            );
        } else {
          $('#comments_warning1').fadeIn(1000, function () { 
            $(this).fadeOut(1000); 
          }); 
        }
    };
    </script>

    <div id="comments_warning1" style="display:none">Don`t forget to fill both fields (Name and Comment)</div>
    <div id="comments_warning2" style="display:none">You can post no more than one comment every 10 minutes (spam protection)</div>
    <form onsubmit="submitComment(this); return false;">
        <table>
            <tr><td class="label"><label>Your name: </label></td><td class="field"><input type="text" value="" title="Please enter your name" id="name" /></td></tr>
            <tr><td class="label"><label>Comment: </label></td><td class="field"><textarea name="text" id="text"></textarea></td></tr>
            <tr><td class="label">&nbsp;</td><td class="field"><input type="submit" value="Post comment" /></td></tr>
        </table>
    </form>
    <div id="comments_list"><?= $sComments ?></div>
  </div>
  <?
  $sCommentsBlock = ob_get_clean();

  } else {
  $sCode .= '<h1>List of items:</h1>';

 $aItems = $GLOBALS['MySQL']->getAll("SELECT * FROM `s163_items` ORDER by  `when` ASC"); // taking info about all items from database
    foreach ($aItems as $i => $aItemInfo) {
    $sCode .= '<h2><a href="'.$_SERVER['PHP_SELF'].'?  id='.$aItemInfo['id'].'">'.$aItemInfo['title'].' item</a></h2>';
   }
  }
  ?>


  <!DOCTYPE html>
  <html lang="en" >
  <head>
    <meta charset="utf-8" />
    <title>Online commenting system</title>
    <link href="css/main.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
</head>
<body>
    <div class="container">
        <?= $sCode ?>
    </div>
    <?= $sCommentsBlock ?>
</body>
</html

请让我知道我哪里出错了。非常感谢各位!

【问题讨论】:

  • php 作为垃圾返回”是什么意思?
  • @RaxWeber,该页面正在呈现为 HTML 页面,但一些 php 代码在页面上显示为文本。
  • &lt;?= 是正确的标签吗?我没见过。
  • @Andreas 对,就是 PHP 速记回显,相当于&lt;?php echo
  • @RaxWeber,要清楚,这是我遇到的错误。图片链接:s15.postimg.org/r593ht9i3/error.png

标签: php html .htaccess


【解决方案1】:

请检查下面的更新代码,请问您的服务器中是否安装了 PHP。

<!DOCTYPE html>
<html lang="en" >
    <head>
        <meta charset="utf-8" />
        <title>Online commenting system</title>
        <link href="css/main.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/jquery-1.5.2.min.js"></script>
    </head>
    <body>
        <?php
        if (version_compare(phpversion(), "5.3.0", ">=") == 1)
            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
        else
            error_reporting(E_ALL & ~E_NOTICE);

        require_once('classes/CMySQL.php');

        $sCode = '';
        $iItemId = (int) $_GET['id'];
        if ($iItemId) { // View item output
            $aItemInfo = $GLOBALS['MySQL']->getRow("SELECT * FROM `s163_items` WHERE `id` = '{$iItemId}'"); // getting info about item from database
            $sCode .= '<h1>' . $aItemInfo['title'] . '</h1>';
            $sCode .= '<h3>' . date('F j, Y', $aItemInfo['when']) . '</h3>';
            $sCode .= '<h2>Description:</h2>';
            $sCode .= '<h3>' . $aItemInfo['description'] . '</h3>';
            $sCode .= '<h3><a href="' . $_SERVER['PHP_SELF'] . '">back</a></h3>';


            $sComments = '';
            $aComments = $GLOBALS['MySQL']->getAll("SELECT * FROM `s163_items_cmts`     WHERE `c_item_id` = '{$iItemId}' ORDER BY `c_when` DESC LIMIT 5");
            foreach ($aComments as $i => $aCmtsInfo) {
                $sWhen = date('F j, Y H:i', $aCmtsInfo['c_when']);
                $sComments .= '<div class="comment" id="' . $aCmtsInfo['c_id'] . '">';
                $sComments .= '<p>Comment from ' . $aCmtsInfo['c_name'] . '<span>(' . $sWhen . ')</span>:</p>';
                $sComments .= '<p>' . $aCmtsInfo['c_text'] . '</p>';
                $sComments .= '</div>';
            }

            ob_start();
            ?>
            <div class="container" id="comments">
                <h2>Comments</h2>
                <script type="text/javascript">
                    function submitComment(e) {
                        var sName = $('#name').val();
                        var sText = $('#text').val();

                        if (sName && sText) {
                            $.post('comment.php', {name: sName, text: sText, id: <?= $iItemId ?>},
                            function(data) {
                                if (data != '1') {
                                    $('#comments_list').fadeOut(1000, function() {
                                        $(this).html(data);
                                        $(this).fadeIn(1000);
                                    });
                                } else {
                                    $('#comments_warning2').fadeIn(1000, function() {
                                        $(this).fadeOut(1000);
                                    });
                                }
                            }
                            );
                        } else {
                            $('#comments_warning1').fadeIn(1000, function() {
                                $(this).fadeOut(1000);
                            });
                        }
                    }
                    ;
                </script>

                <div id="comments_warning1" style="display:none">Don`t forget to fill both fields (Name and Comment)</div>
                <div id="comments_warning2" style="display:none">You can post no more than one comment every 10 minutes (spam protection)</div>
                <form onsubmit="submitComment(this);
                    return false;">
                    <table>
                        <tr><td class="label"><label>Your name: </label></td><td class="field"><input type="text" value="" title="Please enter your name" id="name" /></td></tr>
                        <tr><td class="label"><label>Comment: </label></td><td class="field"><textarea name="text" id="text"></textarea></td></tr>
                        <tr><td class="label">&nbsp;</td><td class="field"><input type="submit" value="Post comment" /></td></tr>
                    </table>
                </form>
                <div id="comments_list"><?= $sComments ?></div>
            </div>
            <?
            $sCommentsBlock = ob_get_clean();
        } else {
            $sCode .= '<h1>List of items:</h1>';

            $aItems = $GLOBALS['MySQL']->getAll("SELECT * FROM `s163_items` ORDER by  `when` ASC"); // taking info about all items from database
            foreach ($aItems as $i => $aItemInfo) {
                $sCode .= '<h2><a href="' . $_SERVER['PHP_SELF'] . '?  id=' . $aItemInfo['id'] . '">' . $aItemInfo['title'] . ' item</a></h2>';
            }
        }
        ?>



        <div class="container">
            <?= $sCode ?>
        </div>
        <?= $sCommentsBlock ?>
    </body>
</html>

【讨论】:

  • 您好 Vishal,我已经尝试在 HTML 正文中添加 php sn-p,但仍然无法正常工作。为了澄清我被抛出的错误,这里是图片链接:s15.postimg.org/r593ht9i3/error.png
  • 我认为 php 没有正确安装/配置,因为 php 代码不执行只是用 html 渲染。我可以访问此页面吗?请分享网址。
  • 再有问题请告诉我文件分机。也是 .php 或 .html
  • 我正在使用 Eclipse 和 Apache Tomcat 服务器。我不确定如何在 Tomcat 上启用 PHP。你能告诉我我应该怎么做吗?谢谢!
  • 文件扩展名为.php
【解决方案2】:

请检查&lt;?php 开始标签。缺少&lt;

【讨论】:

    猜你喜欢
    • 2014-05-11
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多