Home > Database > Mysql Tutorial > How Can I Update a Web Page\'s Content Without Refreshing Using JavaScript and AJAX?

How Can I Update a Web Page\'s Content Without Refreshing Using JavaScript and AJAX?

Susan Sarandon
Release: 2024-11-23 14:11:20
Original
873 people have browsed it

How Can I Update a Web Page's Content Without Refreshing Using JavaScript and AJAX?

Updating Page Contents Without Refreshing Using Javascript and AJAX

You can achieve the functionality of updating the contents of a div without refreshing the page using JavaScript and AJAX. Here's how you can do it:

Since the onclick handlers are executed in the browser, PHP functions cannot be directly invoked. Instead, add a JavaScript function that uses AJAX to fetch data from a PHP script. Using jQuery, you can employ the following code:

function recp(id) {
  $('#myStyle').load('data.php?id=' + id);
}
Copy after login

Create a separate PHP file (data.php in this example) and place your PHP code within it:

require ('myConnect.php');     
$id = $_GET['id'];
$results = mysql_query("SELECT para FROM content WHERE  para_ID='$id'");   
if( mysql_num_rows($results) > 0 )
{
 $row = mysql_fetch_array( $results );
 echo $row['para'];
}
Copy after login

In the HTML, update your links to utilize the JavaScript function:

<a href="#" onClick="recp('1')" > One   </a>
<a href="#" onClick="recp('2')" > Two   </a>
<a href="#" onClick="recp('3')" > Three </a>

<div>
Copy after login

By clicking on these links, the content of the div (with id 'myStyle') will be updated without reloading the page. The jQuery function will dynamically load the data from the PHP script via AJAX, ensuring that the user sees the updated information without having to wait for a page refresh.

The above is the detailed content of How Can I Update a Web Page\'s Content Without Refreshing Using JavaScript and AJAX?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template