繁体   English   中英

如何使用php在其他HTML页面中打开搜索结果

[英]How do I open the search results in a different html page using php

我创建了一个小的搜索栏,它将生成电影详细信息,但是我希望结果在单独的html页面中打开,你们可以帮我解决这个问题吗?

我的代码如下

<div class="form-container">

        <form method="POST">
            <div class="search-container">
                <input type="text" name="search" placeholder="Search...">
                <button class="btn btn-primary" type="submit" name="submit-search">Search</button>
            </div>
        </form> 
        <div class="resutls">
        <?php

            if (isset($_POST['submit-search'])) {

                $txtresult = $_POST['search'];

                if ($txtresult == 'red') {
                    echo "<span class= 'red'>".$txtresult."</span><br>";
                }elseif ($txtresult == 'green') {
                    echo "<span class= 'green'>".$txtresult."</span><br>";
                }

                function    getImdbRecord($title, $ApiKey)
                    {
                        $path = "http://www.omdbapi.com/?t=$title&apikey=$ApiKey";
                        $json = file_get_contents($path);
                        return json_decode($json, TRUE);

                    }
                $data = getImdbRecord($txtresult, "f3d054e8");  
                echo "<span class = 'info-box'><h3> Name :".$data['Title']."</h3><h3> Year : ".$data['Year']."</h3><h3> Duration : ".$data['Runtime'],"</h3></span>";

    }

        ?>
    </div>

您需要在form标记中使用action参数来指定其他HTML / PHP页面。

<form action="otherFile.php" method="POST">

然后,您需要移动代码以将结果获取到otherFile.php

在表单HTML元素中添加action属性。 action属性指定提交表单时将表单数据发送到的位置。 无需在此页面/文件中编写PHP代码。 例:

<div class="form-container">
    <form method="POST" action="searchResult.php">
        <div class="search-container">
           <input type="text" name="search" placeholder="Search...">
           <button class="btn btn-primary" type="submit" name="submit-search">Search</button>
        </div>
   </form>
</div>

然后在该searchResult.php文件中编写PHP代码。 例:

if (isset($_POST['submit-search'])) {
    $txtresult = $_POST['search'];
    if ($txtresult == 'red') {
        echo "<span class= 'red'>".$txtresult."</span><br>";
    }elseif ($txtresult == 'green') {
        echo "<span class= 'green'>".$txtresult."</span><br>";
    }

    function getImdbRecord($title, $ApiKey)
    {
        $path = "http://www.omdbapi.com/?t=$title&apikey=$ApiKey";
        $json = file_get_contents($path);
        return json_decode($json, TRUE);
    }

    $data = getImdbRecord($txtresult, "f3d054e8");

    if (isset($data['Error']) && 'Movie not found!' == $data['Error']) {
        echo "<span class= 'red'>{$data['Error']} by keyword <b>{$txtresult}</b></span><br>";
    } else {
        echo "<span class = 'info-box'><h3> Name :".$data['Title']."</h3><h3> Year : ".$data['Year']."</h3><h3> Duration : ".$data['Runtime'],"</h3></span>";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM