【发布时间】:2020-11-18 18:02:03
【问题描述】:
我是php 的初学者,我正在从事一个前端项目,我必须根据存储在mysql xampp server 中的12 个岛屿名称创建一个刽子手游戏。我必须从数据库中获取一个random 岛作为我的html 中显示的unordered string 并猜测它是哪个岛。我不知道如何使用 php 来实现这一点,因为我是一个完整的初学者,但我已经观看了有关如何使用 php 将数据从 html 表单发送到 sql server 的教程。我想这是相反的任务。
我已经编写了完整的 html css and js 代码来显示我的刽子手游戏,我使用一个简单的单词通过 javascript 随机显示,当你填写 spaces 时会出现一个 submit 按钮。
function hangman(){
var island = "Santorini"; //the given word that is supposed to be found
var t = document.createTextNode(shuffleWord(island))
document.getElementById("hidden-word").appendChild(t);
createSpaces(island);
const inputLists = document.querySelectorAll("input");
document.querySelectorAll("input").forEach(el => {
el.addEventListener('input', evt => {
const showButton = [...inputLists].filter(ip => ip.value.trim() !== '').length === inputLists.length;
document.getElementById('submitbtn').style.display = showButton ? 'block' : 'none';
});
});
}
function shuffleWord (word){
var shuffledWord = '';
word = word.split('');
while (word.length > 0) {
shuffledWord += word.splice(word.length * Math.random() << 0, 1);
}
return shuffledWord;
}
function createSpaces(text){
for(var i=0;i<text.length;i++){
var space = document.createElement("input");
space.setAttribute("class" , "dash");
document.getElementById("hangman-container").appendChild(space);
}
}
.transparent-box{
border:none;
position:absolute;
top:10%;
left:15%;
background-color:black;
height:500px;
width:70%;
opacity: 0.6;
}
.transparent-box p{
color:white;
text-align:center;
}
.transparent-box h1{
color:white;
position: relative;
text-align:center;
font-size:20px;
top:30px;
}
#hangman-container{
position: relative;
width:auto;
top:30%;
left:0%;
background-color: transparent;
display: flex;
flex-direction: row;
justify-content: space-evenly;
}
.dash{
margin:0;
padding:20px;
align-items: flex-start;
width:4%;
border:none;
border-radius: 5%;
background-color: turquoise;
color:red;
font-size:40px;
}
.dash:focus{
opacity:0.8;
}
#submitbtn{
display: none;
position: absolute;
top:200%;
left:80%;
float:right;
}
<body onload=hangman()>
<div class="transparent-box" id="t-box">
<p>Play here </p>
<h1 id="hidden-word">The word is : </h1>
<form id="hangman-container" method="POST">
<button type="submit" class="hide" id="submitbtn">Submit</button>
</form>
</div>
</body>
问题是如何使用 php 从我的数据库中获取随机岛屿名称并显示它,而不是通过 javascript 发送字符串。 感谢您对此的帮助。提前谢谢你。
【问题讨论】:
-
我觉得这不是您需要的问题的具体答案,这是一门 PHP 快速课程。只是谷歌“PHP 从数据库中获取数据”?
标签: javascript php html mysql xampp