我正在制作像股票价格查找器这样的东西,有数据表由股票名称,代码,价格组成。
我想要做的是,如果用户在 index.html 上输入股票名称,结果将显示在 result.php ,如果用户点击股票名称,请描述有关 view.php 的更多信息。无论如何我编写了这个,问题是......我认为分页会因用户而变得复杂,我想将页面组合到一个页面。不使用result.php和view.php,而只使用index.html。
喜欢这样......
页面有一些变量移动页面,所以我非常害怕如何合并这些页面。用户输入文本和代码。我不知道如何控制它。
的index.html
<form action="./result.php" method="get">
<label>
Search
<input type="text" name="keywords">
<input type="submit" value="Search">
</label>
</form>
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
}
}
?>
view.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;
?>
我编写的上述代码。但我想合并它。如何将3个文档合并到1个文档中?因为用户输入'关键字(index.html-&gt; result.php)'和'_GET变量(result.php-&gt; view.php)'这对我来说非常困难。请帮我。
答案 0 :(得分:3)
您可以保留单独的文件,但不是在这些实际文件中显示信息,而是可以通过AJAX(异步HTTP请求)调用它们。这是使用jQuery的语法:
<强>的index.html 强>
<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 -->
<强>的Javascript 强>
$('#search').click(function(){
$.ajax({
url: "result.php",
method: "POST",
data: {keywords: $('#keywords').val()}
}).success(function(result) {
$('#result').html(result);
});
});
<强> result.php 强>
<?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.";
?>
因此,在我上面编写的示例中,您将变量keywords
作为关键字传递。这意味着在result.php
内,您可以通过keywords
访问变量$_POST['keywords']
。
所以基本上,你在目前为止所做的result.php
和view.php
中做了完全相同的事情,但是你将数据返回到index.html
文件而不仅仅是回显它在那个文件中。如果你想在jQuery中阅读更多关于AJAX函数的信息,可以点击链接:jQuery.ajax()。
希望这有帮助,如果你想知道某事,请告诉我。
答案 1 :(得分:1)
这是一个非常广泛的问题。
所以答案是非常主观的。
如果这是你的第一个牛仔竞技表演,我认为现在是了解 AJAX 的好时机。 基本思想是让javascript从服务器请求信息,并将响应添加到页面。
AJAX是一组复杂的方法,但是对于像jquery这样的现代框架,它变得易于实现。
请参阅此文档: http://www.w3schools.com/jquery/jquery_get_started.asp
和 https://api.jquery.com/jquery.get/
将jquery添加到您的网页时,请尝试以下操作:
$.get( "result.php", function( data ) { $( ".result" ).html( data ); alert( "Load was performed." ); });
如果你不喜欢ajax, 尝试将iframe添加到index.html,并将iframe的src设置为要显示的php页面。 这将导致它们加载一次。因此,每次下拉列表更改时都需要刷新它们。