2016-04-21 20 views
0

私はさまざまなコースをリストしている自動補完検索ボックスを作っています。基本的には、選択したコースの結果を別のページに表示するSQLデータベースを使用します。ページは次のようになります:index.phpPHPのあるページから別のページに変数をインポートする

したがって、オートコンプリートから選択したものは、「コース検索」ボタンをクリックして別のページにリンクし、その選択したコースの結果をすぐに検索ボックスに表示します。前のページ。これらの結果は、SQLデータベースから取得されます。

基本的にはすべてをつなぐのに問題があり、それを理解できません。

これは私の検索ボックスとボタン:

<div class="row"> 
    <div class="ui-widget col-md-4 col-md-offset-4"> 
    <label for="tags">Search: </label> 
    <input id="tags" type="search"> 
    <input type="submit" value="Search Courses"> 
    </div> 
</div> 

私は次のページに渡す変数のいくつかの並べ替えを設定する必要がありますか?

ありがとうございました。

これは私のautocomplete.phpファイルです:

<?php 
$mysqli = new mysqli("localhost", "scott", "tiger", "courses"); 
$term = mysqli_real_escape_string($mysqli,$_REQUEST['term']); 
$query = " 
SELECT title 
    FROM course 
WHERE title LIKE '$term%' 
ORDER BY title"; 
$return = array(); 
$result = $mysqli->query($query); 
while ($row = $result->fetch_array()){ 
    array_push($return,$row[0]); 
} 
echo json_encode($return); 
?> 

index.htmlを:それはまだない場合

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Edinburgh Napier University</title> 
    <div class="row"> 
    <div class="col-md-4 col-md-offset-4"> 
    <img id="napierlogo" src="logo.jpg"/> 
    </div> 
    </div>  

    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css"> 

    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 

    <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> 
    <link rel="stylesheet" href="/resources/demos/style.css"> 
<script> 
$(function() { 
$("#tags").autocomplete({ 
    source: "autocomplete.php", 
    autoFocus:true 
}); 
}); 
</script> 

<script> 
    $(function() { 
    $("#menu").menu(); 
    }); 
    </script> 
    <style> 
    .ui-menu { width: 150px; } 
    </style> 

    <script> 
    $(function() { 
    $("input[type=submit]") 
     .button() 
     .click(function(event) { 
     event.preventDefault(); 
     }); 
    }); 
    </script> 

    <?php 
    isset($_GET['res'])?$var=mysql_escape_string($_GET['res']):$res=''; 
    ?> 

</head> 
<body> 

<p></p>  

<div class="row"> 
    <div class="ui-widget col-md-4 col-md-offset-4"> 
    <label for="tags">Search: </label> 
    <input id="tags" type="search"> 
    <input type="submit" value="Search Courses"> 
    </div> 
</div> 

<p></p> 

<ul id="menu"> 
    <li><a href="http://socwebtech1.napier.ac.uk/~40171342/course/index.php">Search</li> 
    <li><a href="http://my.napier.ac.uk/Pages/Home.aspx">myNapier</a></li> 
    <li>Contact Us 
    <ul> 
     <li><a href="https://www.facebook.com/EdinburghNapierUniversity/">Facebook</li> 
     <li><a href="https://twitter.com/EdinburghNapier? ref_src=twsrc%5Egoogle%7Ctwcamp%5Eserp%7Ctwgr%5Eauthor">Twitter</li> 
     <li><a href="https://uk.linkedin.com/edu/edinburgh-napier-university-12617">LinkedIn</li> 
    </ul> 
    </li> 
</ul> 

</body> 
</html> 
+0

これまでに書いたPHP/javascriptコードを表示できますか? –

+0

それを人に追加しました:) –

+0

結果が表示されるはずのresults.phpページもありますが、これまでのindex.phpページ –

答えて

2

まず、あなたのインデックスページは.phpファイルにする必要があります.htmlの代わりに。

第2に、検索ボタンをクリックしたときに送信されるフォームに検索ボックスを変更する必要があります。これによりPOSTリクエストがresults.phpページに送信され、テキストボックスに入力された検索語句を使用してデータベースから結果を読み込むことができます。

あなたのテキストボックスのHTMLコードになるはずです

<div class="row"> 
    <form action="results.php" method="post"> 
    <div class="ui-widget col-md-4 col-md-offset-4"> 
     <label for="tags">Search: </label> 
     <input id="tags" type="search" name="search"> 
     <!-- Added the 'name' attribute^here 
      we'll need this later in the PHP file --> 
     <input type="submit" value="Search Courses"> 
    </div> 
    </form> 
</div> 

そしてresults.phpはこれにトップと類似のいくつかのPHPコードを持っている必要があります。$_POST

<?php 

$searchTerm = $_POST["search"]; 
// Key part -> ^^
// this needs to match the name of the search box in html 

// TODO -- Sanitize this input (NEVER trust user input) 
// using mysqli_real_escape_string() or similar. 
// Then use it to perform your search 

// Just for testing: 
echo $searchTerm; 

詳しい情報はこちら:http://php.net/manual/en/reserved.variables.post.php

補足として、実際にはを使用する必要があります210データベースアクセス。 SQLインジェクションを防止するための「準備済みのステートメント」をはじめとする優れた機能が満載です。これはベストプラクティスであり、将来のメンテナンス性には良いことです。ここ

詳細情報:http://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

1

AJAXとjQueryのオートコンプリートを使用して、sourceリクエストはGETです。変数を$_GET['term']に変更し、入力ボックスにname="term"を入力してみてください。そして、あなたの機能をこのようなものに変更してください。

$(document).on("ready",function() {   
    $("#tags").autocomplete({ 
    source: "autocomplete.php", 
    autoFocus:true 
    }); 
}); 

編集:一部のリンクでも終了タグがありません。あなたのHTMLコードを整理することをお勧めします。

関連する問題