【发布时间】:2016-11-09 06:06:17
【问题描述】:
我正在尝试使用 php 创建一个在线评论系统。这是我的 php 页面,其中包含 HTML。但是,它没有按预期执行,呈现 HTML 元素,而 php 在执行期间作为垃圾返回。我错过了什么吗?我是否使用 .htaccess 文件告诉编译器将其作为纯粹的 php 文件执行?
这是我的代码:
<?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"> </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 代码在页面上显示为文本。
-
<?=是正确的标签吗?我没见过。 -
@Andreas 对,就是 PHP 速记回显,相当于
<?php echo。 -
@RaxWeber,要清楚,这是我遇到的错误。图片链接:s15.postimg.org/r593ht9i3/error.png