前面已经讨论了同样的问题,我也知道如果我们预期调用header函数之前没有任何内容,也不能将空格发送到浏览器。我在这里做错了,之前我正在打印HTML div标签,因此解决方案就是遵循一个基本策略,以便一切都能按照我想要的方式运行!仅供参考,我可以使用AJAX(使用jQuery)通过从被调用的PHP脚本返回JSON编码输出作为响应(它可能是我在下面的代码中显示的结果部分或错误) - 一切都可以通过使用AJAX工作。但由于它是一个搜索页面,我可以使用URL中的search_query的url重写用户,因此如果用户需要,则无需填写表单 - 因此使用GET方法。下面是我的代码我正在实现这个目标:
<div class="two-third">
<?php if($_GET['err'] == "no_results_found") {
echo "<p class='error-box'>No results found for your query. Please search using different specific keywords.</p>";
} ?>
<form method="GET" action="<?php echo BASE_URL; ?>/content.php" id="search_content_form">
<fieldset>
<div>
<label>Search this site: </label>
<input type="text" title="Enter your query" class="form-poshytip" id="search_query" name="search_query" value="<?php echo $search_query; ?>">
</div>
<p><input type="submit" id="search_content_submit_button" value="Search Content"></p>
</fieldset>
</form>
<?php if(isset($search_query)) {
$query = "SELECT * FROM (
(SELECT 'event' AS content_type, event_id AS content_id, event_title AS content_title, publishing_time AS content_publishing_time FROM event WHERE event_title LIKE '%".$search_query."%' OR content LIKE '%".$search_query."%')
UNION
(SELECT 'article' AS content_type, article_id AS content_id, article_title AS content_title, publishing_time AS content_publishing_time FROM article WHERE article_title LIKE '%".$search_query."%' OR content LIKE '%".$search_query."%')
) content_result
ORDER BY content_result.content_publishing_time DESC
LIMIT 500
";
$result = mysql_query($query);
if(!$result) {
header("location:".BASE_URL."/content.php");
}
if(mysql_num_rows($result) == 0) {
header("location:".BASE_URL."/content.php?err=no_results_found");
}
$i = 0;
echo "<div id='search_content_results' class='list'>";
echo "Found <strong>".mysql_num_rows($result)."</strong> result(s) for search query: \"<strong>".$search_query."</strong>\"";
while($row = mysql_fetch_assoc($result)) {
// extract all needed varaibles from $row
$content_type = $row['content_type'];
$content_id = $row['content_id'];
$content_title = $row['content_title'];
$content_publishing_time = $row['content_publishing_time'];
echo "
<ul>
<li><a href='".BASE_URL."/".$content_type."s.php?".$content_type."_id=".$content_id."'>".($i+1).") <strong>".$content_title."</strong> <span style='font-size: 12px; text-decoration: italic;'>(Published On: ".date('M jS Y', strtotime($content_publishing_time)).")</span></a></li>
</ul>
";
$i++;
}
echo "</div>";
}
?>
</div>
<!-- ENDS two-third -->
仅供参考:上面的$ search_query是$ _GET ['search_query']的别名
除了调用header()的if块之外,上面代码中的所有内容都有效。我知道原因,如前所述。只是帮我检查页面顶部的一切,这是我一直在使用的IF块,然后显示搜索表单,然后当num_rows大于0时显示搜索查询的输出或显示错误(如上面的代码中所示)如果num_rows MySQL查询的结果是0.对不起因为我的语言不好(也没有正确的格式化),因为我很着急。 ;)
编辑: 逻辑很简单:
所以请帮助我实现这些目标。
答案 0 :(得分:2)
我不完全确定我是否理解你的问题,但无论如何我都会试一试。
当您使用PHP的header
函数时,您必须确保在调用header
函数之前不要写任何输出。
有时这很难做到。为了解决这个问题,您需要使用输出缓冲。将ob_start()
(docs)作为脚本的第一行,ob_end_flush
(docs)或ob_end_clean
(docs)作为最后一行(或使用标题函数后)。这有效地缓冲了所有输出,这允许您在实际回显任何输出之前调用标头函数(因为所有输出都被缓冲,直到您调用ob_end_flush
或ob_end_clean
)。
答案 1 :(得分:1)
除了使用Joris提到的ob_start
之外,请记住在设置exit
标题后始终die()
或Location
(除非在某些情况下,如果您确切知道什么你正在做。)