クエリ結果をdivのテキストボックスにエコーする方法を理解しようとしています。フォーム(以下を参照)があり、結果は必要に応じてテキストボックスに配置されますが、div(「コンテンツ」と呼ばれます)にエコーバックされません。いくつかの編集を行った後、ここでHTMLのコードを更新しました。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
<form id="search" method="post" action="rating.php">Miles <input name="miles" id="miles" type="text" /> <input name="search1" id="search1" class="btnButton" value="Search" type="submit" /></form>
<div id="content"></div>
<script type="text/javasript" src="jquery-1.9.0.min.js"></script>
<script type="text/javascript">// <![CDATA[
$(document).ready(function(){
//jquery click event
$("#search").on("submit", function(e){
//disable default form submit postpage
e.preventDefault();
//make ajax call to the search.php parsing the search textbox value as query string
$.get("rating.php?miles="+$("#miles").val(), function(result){
//Result will be dumped in the div
$("#content").html(result);
});
});
});
// ]]></script>
</body>
</html>
これで、milesの値がrating.phpに渡され、そこでクエリを実行して、結果をdivに表示されるテキストボックス形式でフォーマットします。
<?php
$hostname = 'somehost.com';
$username = 'ratetable';
$password = 'mypassword';
$miles = (int) $_POST['miles'];
try
{
$db = new PDO("mysql:host=$hostname;dbname=ratetable", $username, $password);
foreach($db->query("SELECT * FROM rates WHERE mileage<={$miles} ORDER BY mileage DESC LIMIT 1") as $row) {
$result= "<input type='text' name='answer' value='" . $row['ratepermile'] . "'>'";
echo $result;
}
}
catch (PDOException $e) {
echo $e->getMessage();
throw($e);
}
?>
結果は、スクリプトにエコーバックされず、結果があるべき場所に配置されないことを除いて、希望どおりにフォーマットされます。divタグを使用する方法や、特定のdivに確実に移動する方法はありますか?
ご覧いただきありがとうございます。