Php 结果页面显示在同一页面上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21718446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:23:59  来源:igfitidea点击:

Php Result page to be displayed on same page

phphtmlmysqlwordpresssearch

提问by Nau Nau head

This is my coding for the search form:

这是我对搜索表单的编码:

<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
    <form action="https://excelforth.com/search1.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Search" />
    </form>
</body>
</html>

The thing is , When I submit this form on my web page , It goes to the php Results page and displays the results. But the thing is that it is just a plain page with just the results displayed.

问题是,当我在我的网页上提交此表单时,它会转到 php 结果页面并显示结果。但问题是它只是一个只显示结果的普通页面。

My search page :

我的搜索页面:

www.mysite/test/certification-database-search/

The Php file is located in my webroot folder . I was told that for wordpress , the individual subpages cannot be edited .

Php 文件位于我的 webroot 文件夹中。有人告诉我,对于 wordpress,无法编辑单个子页面。

How do I :

我如何能 :

1.Display the results on the same page as the search page. Not a completely new page.

1.在与搜索页面相同的页面上显示结果。不是一个全新的页面。

2.Retain the page layout and theme / headers/ footers of the page

2.保留页面布局和主题/页眉/页脚

3.If possible run the .php file in /certification-database-search/ and query it from there. instead of using the one in my webroot folder.

3.如果可能,运行/certification-database-search/中的.php文件并从那里查询。而不是使用我的 webroot 文件夹中的那个。

THANKS!!

谢谢!!

回答by Funk Forty Niner

Put your form and PHP in one page, PHP on top with HTML below, then use action=""

把你的表单和 PHP 放在一个页面上,PHP 在上面,HTML 在下面,然后使用 action=""

Use the variables from the inputs as $var=$_GET['var'];then echo $var;

使用输入中的变量作为$var=$_GET['var'];然后回显$var;

Sidenote: If you want to stop a process, you can use die();or exit();

旁注:如果你想停止一个进程,你可以使用die();exit();

You can put a message inside it; i.e.: die("Enter a search term");

你可以在里面放一条消息;IE:die("Enter a search term");

A basic example:

一个基本的例子:

<?php
if(isset($_GET['submit'])){
$query=$_GET['query'];
echo $query;
}
?>

<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
    <form action="" method="GET">
        <input type="text" name="query" />
        <input type="submit" name="submit" value="Search" />
    </form>
</body>
</html>

An alternative, showing an error message if field is empty:

另一种方法,如果字段为空,则显示错误消息:

<?php
if(isset($_GET['submit'])){
  if(empty($_GET['query'])){
echo "Enter a search term";
  }

$query=$_GET['query'];
echo $query;
}
?>

<html>
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
    <form action="" method="GET">
        <input type="text" name="query" />
        <input type="submit" name="submit" value="Search" />
    </form>
</body>
</html>