Search code examples
phphtmljsonconnection

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


I have created a small search bar which will generate movie details but I want the results to open in a separate html page can you guys please help me this problem?

My code is below

<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>

Solution

  • You need to use the action parameter in form tag to specify a different HTML/PHP Page.

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

    Then you need to move the code to fetch results to otherFile.php