2015-06-29 73 views
2

我正在做类似股价查找器,有数据表由股票名称,代码,价格组成。 enter image description here如何将php文档合并到1个文档中?

什么我想要做的是,如果在的index.html,结果将显示在result.php用户输入股票名称,如果用户点击股票名称,描述的详细信息view.php 。无论如何,我弥补了这一点,问题是...我认为分页会与用户复杂,我想将页面合并到一个页面。不使用result.php和view.php,但只使用index.html。

enter image description here

就像这个...

enter image description here

有一些变更移动一页一页,所以我很害怕我怎么能合并该页面。用户输入文本和代码。我不知道我该如何控制它。

的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-> result.php)'和'_GET变量(result.php-> view.php)'这对我来说非常困难。请帮帮我。

+0

提供从文件的代码示例答案。认为它可以帮助。 –

+0

@Gustaf非常感谢。我会马上尝试。 – Juntae

+1

很高兴能帮到你! –

回答

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内部,您可以通过$_POST['keywords']访问变量keywords

因此,基本上,你做同样的事情在result.phpview.php你这样做的远,但你的数据返回给index.html文件,而不是只是呼应它在该文件中。如果您想了解更多关于jQuery中AJAX函数的信息,请点击以下链接:jQuery.ajax()

希望这有助于,请让我知道,如果你想知道的东西。

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,并设置sr c的iframe到您想要显示的php页面。 这将导致它们加载一次。所以你需要在每次下拉改变时刷新它们。

这可能是一个有点棘手:How to refresh an IFrame using Javascript?

相关问题