Skip to content

Instantly share code, notes, and snippets.

@meech-ward
Created September 16, 2019 17:25
Show Gist options
  • Save meech-ward/f72d101c400692da703fef920355f149 to your computer and use it in GitHub Desktop.
Save meech-ward/f72d101c400692da703fef920355f149 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form id="quoteForm">
<input type="text" name="quote">
<input type="submit" value="Submit">
</form>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
function createQuote(data) {
$.ajax({
type: "POST",
url: "kanye.php",
data: data
}).then(data => {
console.log(data);
}).catch(error => {
console.log("ERROR", error);
});
}
$("#quoteForm").on("submit", function(event) {
event.preventDefault();
const data = $(this).serialize();
createQuote(data);
});
</script>
</body>
</html>
<?php
header("Content-type: application/json");
$quotes = [
"Pulling up in the may bike",
"If I don't scream, if I don't say something then no one's going to say anything.",
"I wish I had a friend like me",
"Style is genderless",
"Believe in your flyness...conquer your shyness.",
"Sometimes you have to get rid of everything"
];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$quote = $_POST["quote"];
$file = fopen("quote.txt", "w");
fwrite($file, $quote);
fclose($file);
echo json_encode(["message" => "Successfully added quote"]);
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = array_rand($quotes);
}
$data = ["quote" => $quotes[$id]];
echo json_encode($data);
} else {
http_response_code(400);
echo json_encode(["error" => "Method not supported"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment