Course: CSCI 1210

Attributes

  • action → an action performed when the form is submitted
    • Typically, sent to the server when the user clicks on the submit button
  • method → HTTP method to be used in the action

Example

<!-- Submits a POST request to /action.php -->
<form action="/action.php" method="post" id="someForm">
	<input type="text" name="query">
	<input type="submit" name="submit">
</form>

Retrieving Data

Server (PHP)

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$query = $_POST["query"];
	// Use query data however needed
}
?>

Client (JS)

<script>
document.getElementById('someForm').addEventListener("submit", (e) => {
	e.preventDefault();
	const formData = new FormData(e.target);
	const query = formData.get("query");
	// Use query data however needed
})
</script>