如何使用 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>
您需要使用表单标记中的操作参数来指定不同的 HTML/PHP 页面。
<form action="otherFile.php" method="POST">
然后您需要将获取结果的代码移动到 otherFile.php
在表单 HTML 元素中添加 action
属性。 action 属性指定在提交表单时将 form-data 发送到哪里。这个page/file不用写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>";
}
}
我创建了一个小的搜索栏,它会生成电影的详细信息,但我希望结果在单独的 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>
您需要使用表单标记中的操作参数来指定不同的 HTML/PHP 页面。
<form action="otherFile.php" method="POST">
然后您需要将获取结果的代码移动到 otherFile.php
在表单 HTML 元素中添加 action
属性。 action 属性指定在提交表单时将 form-data 发送到哪里。这个page/file不用写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>";
}
}