我有一个简单的网页,显示电话卡的信用余额。到目前为止,使用 HTML、PHP 和 mysql 没有任何问题,我能够从数据库中检索余额。但是我必须在另一个页面中显示结果,这看起来很糟糕,因为页面必须重新加载。
我可以将此值加载到从客户那里收集数据的输入字段下的预绘制字段中吗?如 :
帐号:134556 密码:***** |发送|
余额为:12.36 美元
我也认为 AJAX 将是最好的选择。我建议你使用 jQuery javascript 库。它为许多功能提供了一个很好的包装器。我通常在http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktype使用 jQuery/AJAX/POST +jQuery 是跨浏览器兼容和流行的。
尽管我同意 anakata 的观点,但我还是会向您推荐这个。ajax 基础教程。
<form action="your_action" method="POST" id="balance_checker_form">
<input type="text" name="account_id" id="account_id" />
<input type="submit" value="Check Balance" />
</form>
<script type="text/javascript">
$(function(){
$('#balance_checker_form').submit(function(){
$.ajax({
url : $(this).attr('href'),
success : function(html){
// Inject the response into the balance_response div. Might want to do some highlighting or something to let the
// user know that the field was updated
$('#balance_response').html(html);
}
});
return false;
});
});
</script>
<div id="balance_response"></div>
试试看。应该可以帮助您朝着正确的方向开始。您将需要包含 jQuery 库。 http://jquery.com/
您可以使用 KB22 指出的 Ajax。
Ajax 是一种技术,您可以使用它使用 javascript 从服务器获取内容并处理请求/响应等,而无需重新加载页面。它可以在后台发生,并且可以使用一些代码来获取远程获取的内容并将其显示在您想要的位置(在现有页面上)。
这是一个简单的 Ajax 教程http://www.codecoffee.com/articles/ajax.html