0

我在页面上有此代码

`

    <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' ); ?>      

请尽快帮助我

4

1 回答 1

1

如果您直接提交搜索表单search-page.php并将表单验证代码放入该脚本中会更好。

话虽如此,您不需要在此页面上进行 sql/html 转义,因为您没有在 sql 或 html 上下文中使用表单数据。试试这个基本系统:

$keywords = $_POST['keywords'];

header('Location: search-page.php?keywords=' . urlencode($keywords));
于 2013-10-08T15:41:21.243 回答