Skip to content

Instantly share code, notes, and snippets.

@DaniloCeesar
Created May 18, 2021 17:43
Show Gist options
  • Save DaniloCeesar/95c63e7c93e56deb05fb150923d03fe8 to your computer and use it in GitHub Desktop.
Save DaniloCeesar/95c63e7c93e56deb05fb150923d03fe8 to your computer and use it in GitHub Desktop.
Search form example using GET request
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Search form example</title>
</head>
<body>
<form method="GET" id="websearch" name="websearch" action="search_target.php">
<label for="q">Search query:</label>
<input type="text" id="q" name="q"> <br />
<button type="submit">Search</button>
</form>
</body>
</html>
<?php
// Beforehand, check if there's any query informed by the user; if not, then show an error message, and abort all the functions
if ( empty($_GET["q"]) ) {
die("User did not inform any query.");
}
// Here goes your database query functions
// [...]
// Prints the search query on the page
echo(
'Search results for "' . htmlspecialchars($_GET["q"], ENT_QUOTES, "UTF-8") . '":'
);
// Prints the search results
// [...]
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment