清除按钮不清除文本框字段 php

Clear button not clearing textbox field php

提问人:fam 提问时间:11/13/2023 最后编辑:James Zfam 更新时间:11/13/2023 访问量:31

问:

我正在创建一个简单的网站,因为我还在学习。我们有一个活动,包括用户注册、查看注册用户的记录、编辑/更新记录。在更新中,一旦用户单击“编辑”,就会显示一个更新窗体,其文本框中填充了用户要编辑的行的值。我的问题是当用户单击清除按钮时,它不起作用,我需要使用 php 进行清除。


<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "", "ortegadb");
 
// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 
// Attempt select query execution
$id = $_REQUEST["userid"];
$sql = "SELECT * FROM usertb WHERE id=$id";
if($result = mysqli_query($link, $sql)){
    $row = mysqli_fetch_array($result);
            $username = $row["username"];
            $password= $row["password"];
            $fullname = $row["fullname"];
           
    
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
 

// Close connection
mysqli_close($link);
?>  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Update</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            max-width: 400px;
            width: 100%;
        }

        input {
            width: 100%;
            padding: 10px;
            margin: 8px 0;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        input[type="submit"], input[type="reset"] {
            background-color: #4caf50;
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        input[type="submit"]:hover, input[type="reset"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>

    <?php
    // Your PHP code here...
    ?>

<form action="updaterecord.php" method="POST">

<h2><center>Update</center></h2> <br>
Userid:
<input type="text" name="id" value="<?php echo $id ?> "><br>
Username
<input type="text" name="uname" value="<?php echo $username ?> "><br>
Full Name:
<input type="text" name="fullname" value="<?php echo $fullname ?> "><br>
Password:
<input type="text" name="pass" value="<?php echo $password ?> "><br>
<input type="submit" name="sbt" value="Update">
<input type="reset" value="Clear">

</form>
</body>
</html>

我尝试使用javascript,但我们不允许使用它,此外它仍然无法正常工作。我还尝试只制作值“”,但仍然不起作用。

php sql-update 明文

评论

2赞 Kazz 11/13/2023
该元素会将表单中的所有字段设置为其原始状态,当它们不在开始时时,它不会将它们设置为空。您可能会误解您被要求执行的操作。 如果您真的想将所有字段设置为空,请将其更改为提交,为其命名,然后检查其是否存在于帖子数据中,这样您就可以检测用于提交表单的按钮并采取相应的行动。您的代码容易受到 SQL 注入的影响,因为您没有清理来自 的用户输入,在那里使用预准备语句来修复它<input type="reset">type="reset"$_REQUEST["userid"]

答:

0赞 Pippo 11/13/2023 #1

你可以用javascript来做

首先用一个简单的按钮更改重置按钮:

<input id="clear-button" type="button" value="Clear" >

然后在单击时添加侦听器:

<script>

    document.addEventListener("DOMContentLoaded", () => {

        document.getElementById("clear-button").addEventListener("click", (e) => {

            e.target.form.querySelectorAll("input:not([type=button]):not([type=submit])").forEach( (el) => {
                el.value = '';
            })
        });

    });

</script>