我在页面上有这段代码
`
<form method="post" action="" >
<div id="search">
<form method="post" action="" >
<input type="text" name="keywords" placeholder = "Try your luck"/><input type="submit" value="Search" />
</form>
<?php
if(isset($_POST['keywords'])){
$keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));
$errors = array();
if(empty($keywords)){
$errors[] = 'Please enter a search term';
}
else if(strlen($keywords)<3){
$errors[] = 'Your search term must be three or more characters';
}
else if(search_results($keywords) === false){
$errors[] = 'Your search for '.$keywords.' returned no results';
}
if(empty($errors)){
header ('Location: search-page.php');
}
else{
foreach($errors as $error){
echo $error;
}
}
}
?>
问题是我想在searchpage.php页面上使用变量$ keywords。我该怎么做呢?
编辑:search-page.php的代码是:
<?php include_once( 'headersearch.php' );
include ('includes/func.inc.php');
global $variable;
?>
<table id="content-area">
<tr>
<td id="search-page" valign="top">
<?php
$results = search_results($keywords);
$results_num = count($results);
if($results_num == 1){
$s = "result";
}
else{
$s = "results";
}
echo '<p>Your Search for <strong>',$keywords,'</strong> returned <strong>',$results_num,'</strong> ',$s,'</p>';
foreach($results as $results){
?>
<table id="posts">
<tr height="25px"><td><h1><?php echo $results['article_title'] ; ?></h1></td></tr>
<tr><td><h2><span>Posted on</span> :<?php echo date('l jS',$results['article_timestamp']) ?> <span>By</span>: BMC </h2></td></tr>
<tr><td><h3><?php echo substr($results['article_content'], 0, 300) ; ?>[...]</h3></td></tr>
<tr><td><a href="single.php?id=<?php echo $results['article_id']; ?>" title="TEST" class="button">Read Along</a></td></tr>
</table>
<?php
}
?>
<?php include_once('sidebar.php'); ?>
</form>
</td>
</tr>
<?php include_once( 'footer.php' ); ?>
请尽可能快地帮助我
答案 0 :(得分:1)
如果您直接将搜索表单提交给search-page.php
并将表单验证代码放入THAT脚本中,那会更好。
话虽这么说,你不需要在这个页面上进行sql / html转义,因为你没有在sql或html上下文中使用表单数据。试试这个基本系统:
$keywords = $_POST['keywords'];
header('Location: search-page.php?keywords=' . urlencode($keywords));