获取JavaScript提示数据并将其转换为PHP



我有一个涉及网页设计的学校项目,我坚持让我的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>

我猜你想把你的txt值从prompt获取到PHP后端。最常见的方法是使用XMLHttpRequest(又名AJAX,"异步JavaScript和XML"(。下面是一个使用 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上方,那么你可能会在同一个else {}块中调用sendTextToServer(txt); txt = "Hello " + person + "! How are you today?"; 下方。

由于我选择在这里使用 POST,因此它将在 $_POST['txt'] 中显示在 PHP 中。

最新更新