簡體   English   中英

如何將php文檔合並為1個文檔?

[英]How can I combine php documents to 1 document?

我正在做類似股票價格查找器的工作 ,有一個由股票名稱,代碼,價格組成的數據表。 在此處輸入圖片說明

我想做的是,如果用戶在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-> result.php)”和“ _GET變量(result.php-> view.php)”,這對我來說很難。 請幫我。

您可以保留單獨的文件,但是可以在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()

希望對您有所幫助,如果您有任何疑問,請告訴我。

這是一個非常廣泛的問題。

因此,答案將是非常主觀的。

如果這是您的第一個牛仔競技表演,我認為現在是學習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頁面。 這將導致它們加載一次。 因此,您需要在每次下拉列表更改時刷新它們。

這可能有點棘手: 如何使用Javascript刷新IFrame?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM