你知道一种在 div 中动态显示 php 结果而不刷新页面的方法吗? 例如,我们有 2 个 div:一个位于页面上半部分,一个位于页面底部。最上面的一个包含一个带有 3 个输入字段的表单。您在其中输入一些值,然后按按钮。当您按下按钮时,底部 div 显示值而不刷新页面。
最佳答案
你不能用纯 PHP 来做到这一点,因为 PHP 是一种静态语言。您必须使用 Javascript 和 AJAX。我建议使用像 Zepto 或 jQuery 这样的库,以使其易于实现,如下所示:
<form>
<input name="search" />
<input type="submit" />
</form>
<div id="div2"></div>
<script>
// When the form is submitted run this JS code
$('form').submit(function(e) {
// Post the form data to page.php
$.post('page.php', $(this).serialize(), function(resp) {
// Set the response data into the #div2
$('#div2').html(resp);
});
// Cancel the actual form post so the page doesn't refresh
e.preventDefault();
return false;
});
</script>
关于php - 在 div 中显示 PHP 结果而不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17391917/