如何使用 AJAX 更新动态内容而不刷新页面?

Patricia Arquette
发布: 2024-10-22 11:46:03
原创
858 人浏览过

How to Update Dynamic Content without Page Refresh Using AJAX?

Dynamic Content Updates with AJAX

Problem:

You aim to update the content of a div without reloading the page upon clicking a link.

Solution without Refreshing:

Employing AJAX (Asynchronous JavaScript and XML), you can retrieve updated content without reloading the page.

Implementation:

  1. Handle Link Events with JavaScript:

    Assign JavaScript event handlers to links to invoke the data retrieval function.

    <code class="html"><a href="#" onClick="recp('1')" > One   </a></code>
    登录后复制
  2. AJAX Request with jQuery:

    The recp() function uses jQuery's .load() method to retrieve data from a PHP script without reloading the page.

    <code class="javascript">function recp(id) {
      $('#myStyle').load('data.php?id=' + id);
    }</code>
    登录后复制
  3. Retrieve Data with PHP:

    Create a PHP script (e.g., data.php) that retrieves data based on the ID passed in the URL.

    <code class="php"><?php
    require ('myConnect.php');     
    $id = $_GET['id'];
    $results = mysql_query("SELECT para FROM content WHERE  para_ID='$id'");   
    if( mysql_num_rows($results) &gt; 0 )
    {
     $row = mysql_fetch_array( $results );
     echo $row['para'];
    }
    ?></code>
    登录后复制
  4. Render Updated Content:

    The retrieved data is loaded into the div with id='myStyle'.

    <code class="html"><div id='myStyle'>
    </div></code>
    登录后复制

With this implementation, clicking links updates the div's content dynamically, providing a seamless and responsive user experience without refreshing the page.

以上是如何使用 AJAX 更新动态内容而不刷新页面?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!