Skip to content

Instantly share code, notes, and snippets.

@daveh
Created March 1, 2020 17:46
Show Gist options
  • Save daveh/ff121a0bfcfeebcde82081cd2f32e935 to your computer and use it in GitHub Desktop.
Save daveh/ff121a0bfcfeebcde82081cd2f32e935 to your computer and use it in GitHub Desktop.
Checking if a form has been submitted in PHP (https://youtu.be/tORiyyMds1M)
<?php
// Code to accompany this video: https://youtu.be/tORiyyMds1M
// if ($_POST) {
// if (isset($_POST['sendButton'])) {
if($_SERVER['REQUEST_METHOD'] == 'POST') {
exit("form submitted");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form example</title>
</head>
<body>
<h1>Detect form submission</h1>
<form method="post">
<div>
<input type="checkbox" id="agree" name="terms">
<label for="agree">I agree</label>
</div>
<!-- <button type="submit" name="sendButton">Send</button> -->
<button type="submit">Send</button>
</form>
<button id="submitJS">Submit using script</button>
<script>
document.getElementById("submitJS").addEventListener("click", function(event) {
document.querySelector("form").submit();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment