为什么不显示消息

Why isn't this displaying a message?

本文关键字:消息 显示 为什么不      更新时间:2023-09-26

我正在编写一个简单的HTML表单来测试我构建的PHP数据库应用程序。它以可验证的方式将项目添加到数据库,但它拒绝创建显示消息。谁能解释一下发生了什么?

这是我的代码:

<!DOCTYPE html>
<html lang="en-us">
    <head>
        <meta charset="utf-8"/>
        <style>
            textarea {
                width: 90%;
                height: 300px;
                border-radius: 12px;
            }
            
            form {
                max-width: 50%;
                top: 10px;
                bottom: 10px;
                padding: 1%;
                margin: auto;
                height: 880px;
                background: radial-gradient(#888, #999, #aaa, #bbb, #ccc, #ddd, #eee);
            }
            
            label[for=description] {
                display: block;
            }
            
            body {
                background: steelblue;
            }
        </style>
    </head>
    <body>
        <form>
            <label for="name">Name:</label>
            <input id="name" name="name"/>
            <hr/>
            <label  for="description">Description:</label>
            <textarea id="des" name="cost"></textarea>
            <hr/>
            <button onclick="sendData()">Create Class</button>
        </form>
        <script>
            var n;
            var descript;
            function sendData() {
                n = document.getElementById("name").value;
                descript = document.getElementById("des").value;
                var http = new XMLHttpRequest();
                http.onreadystatechange = function() {
                    if (http.readyState == 4 && http.status == 200) {
                        response(http.responseText);
                    }
                };
                http.open("GET", "class_add.php?name=" + name + "&descript=" + descript, true);
                http.send();
            }
         /*This is supposed to make the display msg*/   function response(txt) {
                var p = document.createElement("p");
                p.innerHTML = txt;
                p.style.fontSize = "30px";
                p.style.fontFamily = "Comic Sans MS";
                p.style.color = "magenta";
                document.body.appendChild(p);
            }
        </script>
    </body>
</html>

您正在提交表单,页面正在重新加载并清除任何更改。按钮元素的默认类型是提交,因此请将其更改为按钮,以便单击它不会提交表单:

<button type="button" onclick="sendData()">Create Class</button>