| function generate() { |
| const title = document.querySelector("[name='title']"); |
| const author = document.querySelector("[name='author']"); |
| const story = document.querySelector("[name='story']"); |
| |
| if (!title.value || !author.value || !story.value) { |
| alert("Please enter all required fields"); |
| return; |
| } |
| |
| const json = { |
| title: title.value, |
| author: author.value, |
| story: story.value, |
| }; |
| |
| const xhttp = new XMLHttpRequest(); |
| xhttp.open("POST", "generate.php", true); |
| xhttp.setRequestHeader("Content-type", "application/json;charset=UTF-8"); |
| xhttp.send(JSON.stringify(json)); |
| xhttp.onload = () => { |
| if (xhttp.status === 200) { |
| console.log(xhttp.responseText); |
| const response = JSON.parse(xhttp.responseText); |
| const { status, data } = response; |
| if (status === "ok") { |
| const { id, title, author, story } = data; |
| const message = `Generated story with id ${id} and title ${title} by ${author}.`; |
| console.log(message); |
| } else { |
| console.log("Error generating story"); |
| } |
| } else { |
| console.log("Request failed"); |
| } |
| }; |
| } |
|
|
| function startGame() { |
| window.location.href = `game.php?id=${id}`; |
| } |