1

OK、GoDaddy ホスティング アカウントに jQuery をインストールしました。結果をテキスト ボックスに表示する必要があり、ユーザー入力、ボタン クリック、および結果領域 (div id=status) を取得する HTML を作成しました。ページを更新せずに、結果をボックスに入れたいだけです。私はこれを達成するためにこのjsonのこととAJAXを試しています。私の試みの何が問題なのかを理解しようとしています。ここにHTMLがあります。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1  /DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script language='JavaScript' type='text/javascript'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>

</head>
<body>
<p>Miles <input name="miles" id="miles" type="text" /> <button type="button" name="getdata" id="getdata">Submit</button></p>
<p></p>
<div id="status">

</div>
<script type="text/javascript" language="javascript">
$('getdata').click(function(){

   $.ajax({
           url: 'process.php',
           type:'POST',
           dataType: 'json',
           success: function(output_string){
                   $('#result_table').append(output_string);
               }
           ));

});
</script>
</body>
</html>

process.php ファイルに送信する必要があります。

<?php
$hostname = 'myhost.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) {
   $output_string .= '<input type='text' name='answer' value='" . $row['ratepermile'] . "'>';

   }
}         
echo json_encode($output_string);
catch (PDOException $e) {
   echo $e->getMessage();
   throw($e);
}
?>

これを構築するために例を使用しようとしましたが、HTML フォームでさえ何も起こりません。これの何が問題なのですか?

ご覧いただきありがとうございます。

4

2 に答える 2

1

あなたのセレクターは間違っている$('getdata')はずです。ID を持つ要素を選択するに$('#getdata')は、ID の前に a を付ける必要があります。#また、パラメータを投稿リクエストに渡す必要があります

$.ajax({
       url: 'process.php',
       type:'POST',
       data: {miles: $('#miles').val()},
       dataType: 'json',
       success: function(output_string){
               $('#result_table').append(output_string);
       }
));
于 2013-01-16T20:31:42.603 に答える
0

これを試して、

$('#result_table').val(output_string);
于 2013-03-22T11:09:22.953 に答える