Last active
March 20, 2024 06:43
-
-
Save waimea-cpy/822102d5850c2bbaf7f3468ae7227df3 to your computer and use it in GitHub Desktop.
PHP AJAX - Simple example of AJAX requests handled by PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'common-functions.php'; | |
// Values supplied via the POST request | |
$id = $_POST['id']; | |
// Do the data update | |
$sql = 'UPDATE people | |
SET likes = likes + 1 | |
WHERE id=?'; | |
modifyRecords( $sql, 'i', [$id] ); | |
// No echo required here since data is simply sent TO the server | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'common-functions.php'; | |
// Values supplied via the POST request | |
$id = $_POST['id']; | |
// Request the data | |
$sql = 'SELECT likes | |
FROM people | |
WHERE id=?'; | |
$people = getRecords( $sql, 'i', [$id] ); | |
$person = $people[0]; | |
// Send back data value(s) | |
// In this case it is just a single plain text value | |
// But could use json_encode() if data was more complex | |
echo $person['likes']; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php require_once 'common-functions.php'; ?> | |
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>AJAX Value Update Demo</title> | |
<link rel="stylesheet" href="css/reset.css"> | |
<link rel="stylesheet" href="css/styles.css"> | |
<script src="scripts/likes.js"></script> | |
</head> | |
<body> | |
<header> | |
<h1>AJAX Value Update Demo</h1> | |
</header> | |
<main> | |
<section id="people"> | |
<?php | |
$sql = 'SELECT id, name, likes | |
FROM people | |
ORDER BY name ASC'; | |
$people = getRecords( $sql ); | |
foreach( $people as $person ) { | |
?> | |
<div class="person"> | |
<h3><?= $person['name'] ?></h3> | |
<p>Likes: | |
<span id="likes-<?= $person['id'] ?>"class="likes"> | |
<?= $person['likes'] ?> | |
</span> | |
</p> | |
<button onclick="addLike( <?= $person['id'] ?> );">+1 Like</button> | |
</div> | |
<?php | |
} | |
?> | |
</section> | |
</main> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function addLike( id ) { | |
console.log( `Sending new like for ID=${id}...` ); | |
await sendLike( id ); | |
console.log( `Requesting like count for ID=${id}...` ); | |
await getAndUpdateLikes( id ); | |
} | |
/** | |
* Example of an AJAX request that only SENDS data to the server | |
*/ | |
async function sendLike( id ) { | |
// Prepare the data to include with the AJAX request | |
let dataToSend = new FormData(); | |
dataToSend.append( 'id', id ); // Can append as much data as required | |
// Send the data and wait for a response | |
await fetch( 'add-like.php', { method:'POST', body:dataToSend } ) | |
} | |
/** | |
* Example of an AJAX request that REQUESTS data from the server | |
*/ | |
async function getAndUpdateLikes( id ) { | |
// Prepare the data to include with the AJAX request | |
let dataToSend = new FormData(); | |
dataToSend.append( 'id', id ); // Can append as much data as required | |
// Send the data and wait for a response | |
const response = await fetch( 'get-likes.php', { method:'POST', body:dataToSend } ) | |
// Wait for data to be returned - this will be plain text | |
// Could use respoonse.json() if returning JSON-encoded data | |
const newLikeCount = await response.text(); | |
// Update the on-screen element | |
document.getElementById( 'likes-' + id ).innerHTML = newLikeCount; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment