好的,所以我已经在我的 GoDaddy 托管帐户上安装了 jQuery。我需要在文本框中显示结果并完成我的 HTML,它获取用户输入、按钮单击和结果区域 (div id=status)。我想简单地将结果放在一个框中,而不必刷新页面。我正在尝试这个 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 表单也没有。这有什么问题?
谢谢你看。