我有一个涉及网页设计的学校项目,我很难将我的JavaScript提示数据链接到按钮输入字段,以便我可以将它发送到PHP并进入phpMyAdmin数据库。
以下是我所做的:
<!-- Final Design -->
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="CSS/main.css" >
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<title>Bob Ross and the hunt for the perfect mountain</title>
</head>
<body>
<header> <h1>Bob Ross and the hunt for the perfect mountain</h1> </header>
<div class="container">
<nav>
<ul class="nav nav-pills nav-fill">
<li class="nav-item">
<a class="nav-link active" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link active" id="play_nav" onclick="GetName_LoadPage()">Play (push twice)</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="Pages/Help/Help.html">F.A.Q/Help</a>
</li>
</ul>
</nav>
<div class="content">
<div class="greeting">
<img src="Images/clorox-chan.png" id="clorox_can" alt="clorox chan" width=450px height=550px>
<form method="post" id="username_form" action="db_connection.php">
<input class="button" id="start_button" name="person" type="button" onclick="GetName_LoadPage()" value="Go to game (push twice)">
<br>
<input class="button" id="help_button" type="button" onclick="location.href='Pages/Help/Help.html';" value="Help/F.A.Q" >
</form>
</div>
</div>
</div>
<script>
function GetName_LoadPage() {
var txt;
var person = prompt("Please enter your username:", "Bob Ross-chan");
if (person == null || person == "") {
txt = "You have canclled the prompt.";
}
else {
txt = "Hello " + person + "! How are you today?";
}
document.getElementById("start_button").onclick = function () {
window.location.href = "Pages/Intro/Intro.html";
};
document.getElementById("play_nav").onclick = function () {
window.location.href = "Pages/Intro/Intro.html";
};
}
function goToHelpPage() {
document.getElementById("help_button").onclick = function () {
window.location.href = "Pages/Help/Help.html"
};
}
</script>
</body>
</html>
答案 0 :(得分:0)
我猜你想要从txt
获取prompt
值到PHP后端。最常见的方法是使用XMLHttpRequest
(a.k.a.AJAX,&#34;异步JavaScript和XML&#34;)。以下是使用POST
:
function sendTextToServer(txt) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://myserver.com/myfile.php");
var formData = new FormData();
formData.append("txt", txt);
xhr.addEventListener("readystatechange", function() {
if (xhr.readyState == 4 && xhr.status == 200) { // state DONE, status OK
console.log(xhr.responseText); // your request has been received, do what you want with the result here.
}
});
xhr.send(formData);
}
如果您要在示例中将此功能粘贴到GetName_LoadPage
之上,那么您可能会在同一sendTextToServer(txt);
块内的txt = "Hello " + person + "! How are you today?";
下面else {}
。{ / p>
由于我选择在此使用POST
,因此它会在$_POST['txt']
中显示在PHP中。