php 提交按钮没有反应 不能发送表单

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" href="./css/login.css">
    <link rel="shortcut icon" href="images/logo.png" type="image/x-icon">

    <title> RTC - Cultural and Creative Crowdfunding Community </title>
    <style>
        .error {
            color: red; /* 将错误提示的字体颜色设置为红色 */
        }
    </style>
</head>
<body>

<div class="content-wrapper">
    <div class="image-container">

        <img src="./images/monai.png" alt="">
    </div>
    <div class="message-box">
        <div class="box">

            <img src="./images/RTC.png" alt="Logo" class="logo" onclick="backToHomepage()">

            <div class="title">LOGIN</div>

            <form id="loginForm" class="form" method="POST" action="php/login.php">
                <label for="username">Username</label><span id="usernameError" class="error"></span>
                <input type="text" id="username" name="username"><br>
                <label for="password">Password</label><span id="passwordError" class="error"></span>
                <input type="password" id="password" name="password"><br>
                <p id="captchaMessage" class="captcha-message"><?php echo $_SESSION['captcha']; ?></p>
                <label for="captcha">Captcha</label><span id="captchaError" class="error"></span>
                <input type="text" id="captcha" name="captcha">
                <div id="captchaContainer"></div>
                <input type="button" value="generateCaptcha" onclick="generateCaptcha()"> <br> <br>
                <input type="submit" value="submit">
            </form>
        </div>
    </div>
</div>
<script>
    document.getElementById('loginForm').addEventListener('submit', function (event) {
        event.preventDefault(); // 防止表单默认提交

        const form = document.getElementById('loginForm');
        const formData = new FormData(form);

        const xhr = new XMLHttpRequest();
        xhr.open("POST", "php/login.php", true);
        xhr.onload = function () {
            const response = JSON.parse(this.responseText);
            if (response.success) {
                alert('登录成功');
                window.location.href = '../project-php/HomePage.html';
            } else {
                document.getElementById('usernameError').innerText = response.errors.username || '';
                document.getElementById('passwordError').innerText = response.errors.password || '';
                document.getElementById('captchaError').innerText = response.errors.captcha || '';
            }
        };
        xhr.send(formData);
    });

    function generateCaptcha() {
        const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        const captchaLength = 6;
        let captchaText = '';

        for (let i = 0; i < captchaLength; i++) {
            captchaText += chars.charAt(Math.floor(Math.random() * chars.length));
        }

        const captchaContainer = document.getElementById('captchaContainer');
        captchaContainer.textContent = captchaText;

        // Store captcha in session
        const xhr = new XMLHttpRequest();
        xhr.open("POST", "php/store_captcha.php", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send("captcha=" + captchaText);
    }
    window.onload = generateCaptcha;

</script>
</body>

</html>

在这里插入图片描述
求大神

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值