Podam jako ciekawostkę, że tekst "Something", możesz też "pobrać" w momencie kliknięcia za pomocą ajax-a (fetch)
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My site</title>
</head>
<body>
<div id="container">Text</div>
<button id="btn1">Click me</button>
<script>
document.getElementById('btn1').addEventListener('click', function() {
fetch('twoj_plik_.php')
.then(response => response.text())
.then(data => {
document.getElementById('container').innerHTML = data;
})
.catch(error => console.error('Error:', error));
});
</script>
</body>
</html>
lub
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My site</title>
</head>
<body>
<div id="container">Text</div>
<button id="btn1">Click me</button>
<script>
const btn = document.querySelector('#btn1');
btn.addEventListener('click', showText);
async function showText() {
const container = document.querySelector('#container');
try {
const response = await fetch('twoj_plik_.php');
if (!response.ok)
throw new Error(`HTTP error! Status: ${response.status}`);
container.innerHTML = await response.text();
} catch (error) {
console.error('Error:', error);
}
}
</script>
</body>
</html>
<?php
// twoj_plik_.php
// plik uproszczony maksymalnie na potrzeby demonstracji :-)
echo 'Somethink';
?>