Skip to content

Instantly share code, notes, and snippets.

@julp
Created February 4, 2022 12:47
Show Gist options
  • Save julp/9d68275e9db5bd42bb9451eb888d36f5 to your computer and use it in GitHub Desktop.
Save julp/9d68275e9db5bd42bb9451eb888d36f5 to your computer and use it in GitHub Desktop.
[OC] Variable php vers javascript
<?php
$nonce = base64_encode(random_bytes(20));
header("Content-Security-Policy: script-src 'nonce-{$nonce}'");
?>
<script nonce="<?= $nonce ?>">
function popupsupprimer(id, description) {
console.log([id, description]);
}
var ready = (callback) => {
if (document.readyState != 'loading') callback();
else document.addEventListener('DOMContentLoaded', callback);
}
ready(
() => {
document.querySelectorAll('img.image').forEach(
element => {
element.addEventListener(
'click',
(event) => {
popupsupprimer(event.target.dataset.id, event.target.dataset.description);
}
)
}
);
}
)
</script>
<?php
$db = new PDO(...);
$images = $db->query('SELECT * FROM images');
foreach ($image as $images):
?>
<IMG
class=image
title="icone pour supprimer"
style="height: 30px; width: auto; "
src="images/Supprimer.png"
alt="icone pour supprimer"
data-id="<?= $image['id'] ?>"
data-description="<?= htmlspecialchars($image['description']) ?>"
/>
<?php endforeach ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment