php - 在 div 中显示 PHP 结果而不刷新

标签 php html

你知道一种在 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/

相关文章:

php - 使用 SSL 将 laravel 5 连接到 AWS RDS

html - CSS <li> 不扩展

javascript - 如何在不使用 rel ="stylesheet"的情况下添加 Bootstrap 下拉列表

javascript - 在交替点击时在 2 种颜色之间切换 div 的背景颜色

css - 为什么逗号分隔的占位符规则没有在 css 中应用?

PHP Doctrine : object toArray() method

php - mysql - 设计表所需的理论帮助

php - Symfony2 扩展了 Exception 类

javascript - jQuery .on() 的表现与预期不同

html - 如何更改提交按钮的外观?