Edit this file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Demo</title>
<style>
form input {
display: block;
}
</style>
</head>
<body>
<div class="container"></div>
<form id="myform">
<label for="email">Email</label>
<input type="email" name="email">
<label for="subject">Subject</label>
<input type="text" name="subject">
<label for="message">Message</label>
<input type="text" name="message">
<input type="submit" value="new message">
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$("form#myform").on('submit', function(event) {
// Prevent postback
event.preventDefault();
// Serialize data
formdata = $("form#myform").serialize();
// HTTP POST
$.post('http://127.0.0.1:8000/contact/', formdata,
function(response) {
// console.log(response);
}
).done(function() {
alert("response: ok.");
})
.fail(function() {
alert("error");
})
.always(function() {
console.log('finished');
});
// Clear form
$(event.target).trigger("reset");
// Reload
reload();
});
function reload() {
// HTTP GET
$.getJSON("http://127.0.0.1:8000/contact/", function(data) {
data = data.results;
// Clear list
$('.container').html('');
// Iterate items
var items = [];
$.each(data, function(index, val) {
items.push("<li id='" + 1 + "'>" + val.email + ' said: ' + ' <br/>subject: ' + val.subject + '<br/>' + val.message + '<hr/>' + "</li>");
});
// Add to UI
$("<ul/>", {
"class": "my-new-list",
html: items.join("")
}).appendTo(".container");
});
}
// Run for first time
reload();
</script>
</body>
</html>