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 中国語 Web サイトの他の関連記事を参照してください。

ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!