如何将php文档合并为一个文档?
我制作了一些类似于股票价格查找器的东西,有一个由股票名称、代码、价格组成的数据表 我想做的是,如果用户在index.html上输入股票名称,结果将显示在result.php,如果用户单击股票名称,请描述有关view.php的更多信息。不管怎么说,这是我编的,问题是。。。我认为分页对于用户来说会很复杂,我想把页面合并成一个页面。不使用result.php和view.php,只使用index.html 像这样… 有一些变量在页面之间移动,所以我非常担心如何合并这些页面。用户输入文本和代码。我不知道怎样才能控制它 index.html如何将php文档合并为一个文档?,php,html,Php,Html,我制作了一些类似于股票价格查找器的东西,有一个由股票名称、代码、价格组成的数据表 我想做的是,如果用户在index.html上输入股票名称,结果将显示在result.php,如果用户单击股票名称,请描述有关view.php的更多信息。不管怎么说,这是我编的,问题是。。。我认为分页对于用户来说会很复杂,我想把页面合并成一个页面。不使用result.php和view.php,只使用index.html 像这样… 有一些变量在页面之间移动,所以我非常担心如何合并这些页面。用户输入文本和代码。我不知
<form action="./result.php" method="get">
<label>
Search
<input type="text" name="keywords">
<input type="submit" value="Search">
</label>
</form>
<input type="text" id="keywords">
<button id="search">Search</button>
<div id="result"></div> <!-- The div that will be filled with the result from the AJAX request -->
搜索
result.php
<?php
require_once './require/db.php';
if(isset($_GET['keywords'])){
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query("SELECT name,code FROM data2 WHERE name LIKE '%{$keywords}%' ");
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results.
</div>
<?php
}
if($query->num_rows) {
while($r = $query->fetch_object()) {
?>
<div class="result">
<a href="./result.php?code=<?php echo $r->code;?>">
<?php echo $r->name; ?></a>
</div>
<br />
<br />
<?php
}
}
?>
<?php
require_once("./require/file_get_contents_curl.php");
$code = $_GET['code'];
$source = file_get_contents("http://www.nasdaq.com/symbol/$code");
$dom = new DOMDocument();
@$dom->loadHTML();
$stacks = $dom->getElementsByTagName('dl')->item(0)->textContent;
?>
<?php
require_once './require/db.php';
if(isset($_POST['keywords'])){
$keywords = $db->escape_string($_POST['keywords']);
$query = $db->query("SELECT name,code FROM data2 WHERE name LIKE ' {$keywords}%' ");
echo "Found ".$query->num_rows." results.";
?>
您可以保留单独的文件,但是您可以通过AJAX(异步HTTP请求)调用这些文件,而不是在这些实际文件中显示信息。以下是使用jQuery的语法:
index.html
<form action="./result.php" method="get">
<label>
Search
<input type="text" name="keywords">
<input type="submit" value="Search">
</label>
</form>
<input type="text" id="keywords">
<button id="search">Search</button>
<div id="result"></div> <!-- The div that will be filled with the result from the AJAX request -->
result.php
<?php
require_once './require/db.php';
if(isset($_GET['keywords'])){
$keywords = $db->escape_string($_GET['keywords']);
$query = $db->query("SELECT name,code FROM data2 WHERE name LIKE '%{$keywords}%' ");
?>
<div class="result-count">
Found <?php echo $query->num_rows; ?> results.
</div>
<?php
}
if($query->num_rows) {
while($r = $query->fetch_object()) {
?>
<div class="result">
<a href="./result.php?code=<?php echo $r->code;?>">
<?php echo $r->name; ?></a>
</div>
<br />
<br />
<?php
}
}
?>
<?php
require_once("./require/file_get_contents_curl.php");
$code = $_GET['code'];
$source = file_get_contents("http://www.nasdaq.com/symbol/$code");
$dom = new DOMDocument();
@$dom->loadHTML();
$stacks = $dom->getElementsByTagName('dl')->item(0)->textContent;
?>
<?php
require_once './require/db.php';
if(isset($_POST['keywords'])){
$keywords = $db->escape_string($_POST['keywords']);
$query = $db->query("SELECT name,code FROM data2 WHERE name LIKE ' {$keywords}%' ");
echo "Found ".$query->num_rows." results.";
?>
这是一个非常广泛的问题。
所以答案是非常主观的
如果这是你的第一次牛仔竞技表演,我认为这是学习AJAX的好时机。
基本思想是让javascript从服务器请求信息,并将响应添加到页面
AJAX是一组复杂的方法,但是使用像jquery这样的现代框架,它变得很容易实现
请参阅本文档:
及
将jquery添加到页面时,请尝试以下操作:
如果您不喜欢ajax,
尝试将iframe添加到index.html,并将iframe的src设置为要显示的php页面。
这将导致它们加载一次。因此,每次下拉列表更改时,您都需要刷新它们
这可能有点棘手:提供了一个答案,其中包含了文件中的代码示例。“我想这会有帮助的。”古斯塔夫,非常感谢。我现在就试试。@Gustaf这对我来说有点难。。。我应该这样编码吗?将
添加到
@Gustaf我解决了这个问题。谢谢